Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6842315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:05:06+00:00 2026-05-27T00:05:06+00:00

How do you read lines from a vector and compare their lengths? I have

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:05:07+00:00Added an answer on May 27, 2026 at 12:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to read at most 20 lines from a csv file: rows =
I have a file that I read from, it contains a bunch of lines
I want to read lines from a file into a Set or List. Is
I'm using std::getline() to read lines from an std::istream-derived class, how can I move
I need to read some large files (from 50k to 100k lines), structured in
I want to be able to read line by line from a pdf, compare
i wanna read all the lines from a text so am doing this int
I'd like to read line from file with fstream (I used this before with
How do I read data from a file if my file is like this
I am trying to learn to free pointer in a vector, I have read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.