gpt4 book ai didi

c++ - 将 vector 传递给cpp中的多个文件

转载 作者:行者123 更新时间:2023-11-30 01:43:58 25 4
gpt4 key购买 nike

我不确定将 vector 传递给多个文件的正确方法。该代码对您来说可能看起来一团糟,实际上我试图放置最少的可重现代码。在文件中 match_read.cpp ,我正在尝试使用 vector 元素 std::vector<int> intref ,但我在 vector 中找不到任何东西。文件 perm.cpp , index_read.cpp , 和 index_ref.cpp工作正常。打电话时 match_read.cpp ,代码不适用于第 4 个文件。我不确定是否将 vector 作为参数传递给其他 cpp文件。

main.cpp

#include "index_read.h"
#include "perm.h"
#include "index_ref.h"
#include "match_read.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


int main()

{
int lenght = 3;

char str[] = {'A', 'G', 'C', 'T'};

int pos; // to hold positions of a vector element

int n = sizeof str;

for (int k = 1; k <= lenght; k++) //here we loop through all the possible lenghts 1, 2 and 3

{

print_str(str, "", n, k); //Note: this function works on all cases and not just the case above
}


std::string* permut_array = new std::string[NumberOfPermutations]; // the array that we will use to store the permutations in

std::copy(permutations.begin(), permutations.end(), permut_array); // here we copy the vector into the array



for (int k = 0; k < NumberOfPermutations; k++) //if you want you can use your array to print the permutation.

{
std::cout << permut_array[k] << std::endl;
}

std::cout<<"Total number of permutations is: "<<NumberOfPermutations<<std::endl;

index(permutations, str, read, intread); // calling index function

refindex(permutations, ref, refarr, intref); // calling reference index function

align_read(intref);

//align_read(permutations, refarr, intread, intref, matchedread, found); // matching DNA read with reference genome

return 0;
}

perm.cpp

#include "perm.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int NumberOfPermutations = 0;

std::vector<std::string> permutations;

// The main recursive method to print all possible strings of length "length"
void print_str(const char str[],std::string prefix, const int n, const int lenght)

{

if (lenght == 1)

{

for (int j = 0; j < n; j++)

{
// i commented this ligne so that if you want to use your array to print your permutations you will not get a screnn with permutations printed 2 times
//std::cout << prefix + str[j] << std::endl;
permutations.push_back(prefix + str[j]); // the vector that we will use to store the permutations in
}

}//Base case: lenght = 1, print the string "lenght" times + the remaining letter

else

{


// One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
for (int i = 0; i < n; i++)

// Next character of input added
print_str(str, prefix + str[i], n, lenght - 1);
// "lenght" is decreased, because we have added a new character

}
NumberOfPermutations = permutations.size();
}

perm.h

#ifndef perm_h
#define perm_h

#include <iostream>
#include <string>
#include <vector>

void print_str(const char*,std::string,const int, const int);

extern int NumberOfPermutations;// = 0;

extern std::vector<std::string> permutations;

#endif

index_read.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "index_read.h"


std::string str = "AGCTGTACGCG";
int read[4];

std::vector<int> intread;


void index(std::vector<std::string> permutations, std::string, int read[4], std::vector<int> intread) // to get the index of a string from the permutation.
{

for( int i= 0; i<str.length(); i +=3)
{
std::cout<<"i: "<<i<<std::endl;
std::string str2 = str.substr (i,3);
std::cout<<"substring : "<<str2<<std::endl;
int pos1 = std::find(permutations.begin(), permutations.end(), str2) - permutations.begin();
intread.push_back(pos1); // pushing index of string into vector i.e. storing read into integer representation

read[i] = pos1;
std::cout<<"Read elements:"<<read[i]<<std::endl;
//std::size_t pos = str.find(str2);
std::cout<<"Position of "<<str2<< " : "<<pos1<<std::endl;

}

}

index_read.h

#ifndef index_read_h
#define index_read_h

#include <iostream>
#include <string>
#include <vector>

void index(std::vector<std::string> permutations, std::string, int read[4], std::vector<int> intread);

extern std::vector<int> intread;

extern std::string str;

extern int read[];

#endif

index_ref.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "index_ref.h"
//#include "variables.h"

//std::vector<std::string> permutations ;

std::string ref = "AAGCAGCTGTACGCT";

int refarr[3][6];

std::vector<int> intref;

void refindex (std::vector<std::string> permutations, std::string, int refarr[3][6], std::vector<int> intref) // to get the index of a string from the permutation.
{
for( int j = 0; j < 3; j++) // looping for maning n frames, where n is number of characters together
{ int k = 0; // Tracking the columns of array
std::string str4 = ref.substr(0, j+1);
std::cout<<"substring first : "<<str4<<std::endl;
int fstpos = std::find(permutations.begin(), permutations.end(), str4) - permutations.begin();
intref.push_back(fstpos);
std::cout<<"Fst element:"<<fstpos<<std::endl;
refarr[j][0] = fstpos;
std::cout<<"pos of fst element:"<<refarr[j][0]<<std::endl;
for( int i = j+1; i < ref.length(); i +=3) //loop for breaking the string into substrings
{ k = k+1;
std::cout<<"i: "<<i<<std::endl;
std::string str3 = ref.substr (i,3);
std::cout<<"substring : "<<str3<<std::endl;
int pos2 = std::find(permutations.begin(), permutations.end(), str3) - permutations.begin();
refarr[j][k] = pos2;
std::cout<<"printing array elements: "<<refarr[j][k]<<std::endl;
intref.push_back(pos2); // pushing index of string into vector i.e. storing reference into integer representation
//std::size_t pos = str.find(str2);
std::cout<<"Position of "<<str3<< " : "<<pos2<<std::endl;

}
}

}

index_ref.h

#ifndef index_ref_h
#define index_ref_h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void refindex (std::vector<std::string> permutations, std::string, int refarr[3][6], std::vector<int> intref); // function decleration for indexing the reference. string

extern std::string ref;

extern std::vector<int> intref;

extern int refarr[3][6];

#endif

ma​​tch_read.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "match_read.h"


//std::vector<std::string> matchedread; // vector to store the matched read.

//bool found = false;

void align_read(std::vector<int> intref)

{
int offset;
std::cout<<"testing the error file: "<<std::endl;

for (std::vector<int>::const_iterator i = intref.begin();
i != intref.end(); ++i)
{
std::cout<<"Integer representation of reference: "<<*i<<std::endl;
}
}

ma​​tch_read.h

#ifndef match_read_h
#define match_read_h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

extern std::vector<int> intref;

void align_read(std::vector<int> intref);

#endif

最佳答案

您的 vector 被定义为全局变量和两个函数的参数。这些是不同的变量,因此当填充一个并访问另一个时,您看不到任何内容。

您应该只使用全局变量并去掉两个函数的参数,或者将 vector 作为参数传递,这意味着您不再需要 vector 的 extern 声明。

关于c++ - 将 vector 传递给cpp中的多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com