将矢量传递到cpp中的多个文件

Passing vector to multiple files in cpp

本文关键字:文件 cpp      更新时间:2023-10-16

我不确定将矢量传递到多个文件的正确方式。对你来说,代码可能看起来一团糟,事实上,我试着把可复制的代码放在最低限度。在文件match_read.cpp中,我试图使用矢量元素std::vector<int> intref,但在矢量中找不到任何内容。文件perm.cppindex_read.cppindex_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

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

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

全局变量的问题在于,由于每个函数都可以访问这些变量,因此越来越难以确定哪些函数实际读取和写入这些变量。外部变量是在程序运行后初始化的,在关闭应用程序之前不会关闭。你的IDE是什么?