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 6715965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:40:37+00:00 2026-05-26T08:40:37+00:00

I have a std::vector<Word> data that is off of the struct below: struct Word

  • 0

I have a std::vector<Word> data that is off of the struct below:

struct Word
{
    std::string word;
    int line_number;
};

I have read in words from a file and pushed it in to my vector storing the words in the string above along with the line number that the word appears on. Now I need to sort the words alphabetically and I attempt the following:

    std::sort(data.begin(), data.end());

However when I try to compile the following I get a crazy long list of errors. I believe this is due to the sort algorithm trying to compare the vector.begin() to vector.end() but it doesn’t know how to evaluate the struct word to another struct word.

However neither do I. I am stumped on how to compare the string contained with the structs in the vector.

  • 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-26T08:40:37+00:00Added an answer on May 26, 2026 at 8:40 am

    In this scenario you should write a function that compares two Word structs and pass that function to std::sort.

    bool compare_by_word(const Word& lhs, const Word& rhs) {
        return lhs.word < rhs.word;
    }
    
    std::sort(data.begin(), data.end(), compare_by_word);
    

    In this question you can find solution if you want to write a generic comparator for comparing objects based on an attribute.

    Update Since we’ve had C++11 and C++14 for a while now, I’m adding a solution using a lambda, because that is probably the better practice now:

    std::sort(data.begin(), data.end(), [](const Word& lhs, const Word& rhs) {
        return lhs.word < rhs.word;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
I have a std::vector<std::string> that I need to use for a C function's argument
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > > I'm trying to sort it with std sort,
I have a std::vector<int> , and I want to delete the n th element.
I have a std::vector. I want to create iterators representing a slice of that
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have a std::vector<std::string> which would contain numbers and characters (single char). I would

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.