2011-11-23 162 views
0

如何讀取矢量中的線條並比較它們的長度?我在一個向量中推送了字符串,現在想找到最長的行並將其用作我的輸出。我有這個作爲我的代碼一路攀升到比較字符串:比較矢量中的線條<string>

ifstream code_File ("example.txt"); 

size_t find_Stop1, find_Stop2, find_Stop3, find_Start; 
string line; 
vector<string> code_Assign, code_Stop; 


if (code_File.is_open()) { 
    while (getline(code_File,line)) { 



     find_Start = line.find("AUG");      // Finding all posssible start codes 
     if (find_Start != string::npos) { 
      line = line.substr(find_Start); 
      code_Assign.push_back(line);       //adding line to Code_Assign 


      find_Stop2 = line.find("UGA");        // Try and find stop code. 
       if (find_Stop2 != string::npos) { 

        line = line.substr(line.find("AUG"), find_Stop2); 
        code_Stop.push_back(line);       // Adding it to code_Stop vector 
               } 

      find_Stop1 = line.find("UAA");        // finding all possible stop codes. 
       if (find_Stop1 != string::npos) { 
        line = line.substr(line.find("AUG"), find_Stop1);  // Assign string code_1 from start code to UGA 
        code_Stop.push_back(line);       //Adding to code_Stop vector 
               } 


      find_Stop3 = line.find("UAG");        // finding all possible stop codes. 
       if (find_Stop3 != string::npos) { 
        line = line.substr(line.find("AUG"), find_Stop3); 
        code_Stop.push_back(line);       //Adding to code_Stop vector 
               } 


      } 



    } 

    cout << '\n' << "Codes to use: " << endl; 
     for (size_t i = 0; i < code_Assign.size(); i++) 
      cout << code_Assign[i] << endl; 

    cout << '\n' << "Possible Reading Frames: " << endl; 
     for (size_t i = 0; i < code_Stop.size(); i++) 
      cout << code_Stop[i] << endl; 

    cout << endl; 


    std::vector<std::string>::iterator longest = std::max_element(code_Stop.begin(), code_Stop.end, compare_length); 

    std::string longest_line = *longest; // retrieve return value 



    code_File.close(); 


} 
else cout << "Cannot open File."; 

,試圖澄清我的電流輸出是這一切的code_Stop載體:

可能的閱讀框:

AUG GGC CUC GAG ACC CGG GUU UAA AGU AGG

AUG GGC CUC GAG ACC CGG GUU

AUG AAA UUU GGG CCC AGA GCU CCG GGU AGC GCG UUA CAU

我只想得到最長的一行。

請注意,我只是學習矢量,所以請善待......我一直從這個委員會得到很多幫助,非常感謝。

Ed。我改變了代碼以顯示我放置的位置,它給了我「程序接收到的信號:'EXC_BAD_ACCESS'」。我做了什麼?

回答

3

這應該工作:

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

bool compare_length(std::string const& lhs, std::string const& rhs) { 
    return lhs.size() < rhs.size(); 
} 

int main() { 
    std::vector<std::string> lines; // fill with data 
    std::vector<std::string>::iterator longest = std::max_element(
              lines.begin(), lines.end(), 
              compare_length); 
    std::string longest_line = *longest; // retrieve return value 
} 

compare_length是比較兩個字符串給出的長度的函數。如果第一個字符串比第二個字符串短,則返回true,否則返回false

std::max_element是一種標準算法,它使用指定的比較函數查找序列中最大的元素。 lines.begin()lines.end()將迭代器返回到序列的開始和結尾lines,從而指定算法應該掃描的範圍。

+0

對不起,但你可以詳細說明更多... – user1046825

+0

@ user1046825:我添加了一個解釋。如果你仍然不明白,請告訴你有什麼問題。 –

+0

這可能是一個簡單的問題,但是哪些數據符合要求?這是我已經有的輸出了嗎? – user1046825