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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:13:42+00:00 2026-05-25T22:13:42+00:00

i am trying to sort vector of struct’s elements,but i can’t construct vector itself

  • 0

i am trying to sort vector of struct’s elements,but i can’t construct vector itself here is code

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct student_t{
      string name;
  int age,score;
}   ;

bool compare(student_t const &lhs,student_t const &rhs){
    if (lhs.name<rhs.name)
         return true;
    else if (rhs.name<lhs.name)
        return false;

    else
        if (lhs.age<rhs.age)
            return true;
        else if (rhs.age<lhs.age)
             return false;
    return lhs.score<rhs.score;

}
int main(){

               struct student_t st[10];







 return 0;
}

when i declared vector<student_t>st i can’t access element of struct,please give me hint how to do it

  • 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-25T22:13:43+00:00Added an answer on May 25, 2026 at 10:13 pm
    std::vector<student_t> st;
    for(unsigned i = 0; i < 10; ++i) st.push_back(student_t());
    std::sort(st.begin(), st.end(), &compare);
    

    You could also use this vector constructor instead of lines 1-2:

    std::vector<student_t> st (10 /*, student_t() */);
    

    Edit:

    If you want to enter 10 students with the keyboard you can write a function that constructs a student:

    struct student_t &enter_student()
    {
         student_t s;
         std::cout << "Enter name" << std::endl;
         std::cin >> s.name;
         std::cout << "Enter age" << std::endl;
         std::cin >> s.age;
         std::cout << "Enter score" << std::endl;
         std::cin >> s.score;
         return s;
    }
    std::vector<student_t> st;
    for(unsigned i = 0; i < 10; ++i) st.push_back(enter_student());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compile this code with GCC 4.5.0: #include <algorithm> #include <vector> template
I was trying to sort a vector of struct.The following is my code.I am
I'm trying to sort a Vector in java but my Vector is not a
I am trying to sort but there is a nil. How can i get
I am trying to sort a vector of custom struct in C++ struct Book{
I'm trying to sort a vector that contains an int and a string in
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > > I'm trying to sort it with std sort,
I am trying to sort a list using delegates but I am getting a
I'm trying to sort a multidimensional array in objective-c i know that i can
Within a class, I am trying to sort a vector, by passing a method

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.