我不确定将矢量传递给多个文件的正确方法。代码可能看起来完全混乱,实际上我试图把最小的可重现代码。在文件match_read.cpp
中,我正在尝试使用向量元素std::vector<int> intref
,但我在向量中找不到任何内容。文件perm.cpp
,index_read.cpp
和index_ref.cpp
运行正常。在调用match_read.cpp
时,代码不适用于第4个文件。我不确定将向量作为参数传递给其他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
match_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;
}
}
match_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
答案 0 :(得分:2)
全局变量的问题在于,由于每个函数都可以访问这些函数,因此要确定哪些函数实际读取和写入这些变量变得越来越困难。 extern变量在程序运行后初始化,在关闭应用程序之前不会关闭。 你的IDE是什么?
答案 1 :(得分:2)
您的向量既被定义为全局变量,也被定义为两个函数的参数。这些是不同的变量,因此当填写一个并访问另一个时,您看不到任何内容。
您应该只使用全局变量并删除两个函数的参数,或者将向量作为参数传递,这意味着您不再需要向量的extern
声明。