Reputation: 45
How do you read lines from a vector and compare their lengths? I have pushed strings inside a vector and now would like to find the longest line and use that as my output. I have this as my code all the way up to comparing the strings:
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.";
to try and clarify my current output is this all in the code_Stop vector:
Possible Reading Frames:
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
and I would just like to get the longest line.
Note, I am just learning about vectors, so please be kind...I have been getting a lot of help from this board and really much appreciated.
Ed. I changed the code to show where I have put it and it is giving me "Program received signal: 'EXC_BAD_ACCESS'". What have I done?
Upvotes: 0
Views: 1292
Reputation: 76886
This should work:
#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
is a function that compares the length of two given strings. It returns true
if the first string is shorter than the second one, and false
otherwise.
std::max_element
is a standard-algorithm that find the largest element in a sequence using the specified comparison-function. lines.begin()
and lines.end()
return iterators to the beginning and the end of the sequence lines
, thus specifying the range the algorithm should scan.
Upvotes: 3