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

  • SEARCH
  • Home
  • 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 8927933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:19:11+00:00 2026-06-15T08:19:11+00:00

I have this loop for(int i=0;i<vec1.size();++i) { if(vec1[i]==*p) { vec1[i]=*p; cout<<element updated<<endl; } else

  • 0

I have this loop

for(int i=0;i<vec1.size();++i)
{
    if(vec1[i]==*p)
    {
        vec1[i]=*p;
        cout<<"element updated"<<endl;
    }
    else
    {
        cout<<"push_back"<<endl;
        vec1.push_back(*p);
    }
}

I’m inserting objects in container class and I’ve overloaded the == to check two parameters inside the object and if they match I want to update the them and if they don’t match I want to put them in the vector, but I don’t seem to be able to properly populate my vector, when I do vec1.size() I get 0 even when I insert 3 objects.

  • 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-06-15T08:19:13+00:00Added an answer on June 15, 2026 at 8:19 am

    You’re problem is that your if is inside your search loop. Your if will never be executed, because your loop body never runs, because your .size() will never be greater than 0.

    Try this:

    // UNTESTED
    std::vector<person> vec1;
    add(person *p) {
      std::vector<person>::iterator it = std::find(vec1.begin(), vec1.end(), *p);
      if(it == vec1.end())
        vec1.push_back(*p);
      else
        *it = *p;
    }
    

    Or, if you really want to code the loop by hand:

    // UNTESTED
    std::vector<person> vec1;
    add(person *p) {
      int i;
      for(i=0;i<vec1.size();++i) {
        if(vec1[i] == *p)
          break;
      }
      if(i == vec1.size())
        vec1.push_back(*p);
      else
        vec1[i] = *p;
    }
    

    Of course, you might consider changing your container. Using a std::map would shorten your code and reduce the time it takes to manipulate large data sets.

    std::map<std::string, person> map1;
    add(person *p) {
      map1[p->name] = *p;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this for loop: for(int l = 0; l < level_max; ++l) {
Let's say I have a loop like this: vector<shared_ptr<someStruct>> vec; int i = 0;
I currently have this kind of loop while(1) { generate_string(&buffer); for(int i = 0;
I have this parallel for loop struct p { int n; double *l; }
I have this for loop: int iPanelNumber = 1; foreach (string[] Persons in alItems)
I have this foreach loop: var includedElements = new HashSet<int>(); foreach(var e in elements)
i have this iterator loop, typedef boost::unordered_map<std::pair<int, int>, NavigationNode> NodesMap; NodesMap nodes; for (NodesMap::iterator
I have this loop method to display a ViewPager and I set an OnClickListener
I have this loop for (it= someCollection.iterator; it.hasNext(); ) { //some code here }
So I do have this loop, but it stops when it come to function

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.