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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:23:54+00:00 2026-05-14T05:23:54+00:00

Please take a look at this example: #include <iostream> #include <vector> #include <string> using

  • 0

Please take a look at this example:

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

class mySubContainer
{
public:
    string val;
};

class myMainContainer
{
public:
    mySubContainer sub;
};

void doSomethingWith( myMainContainer &container )
{
    container.sub.val = "I was modified";
}

int main( )
{
    vector<myMainContainer> vec;


    /**
     * Add test data
     */
    myMainContainer tempInst;

    tempInst.sub.val = "foo";
    vec.push_back( tempInst );

    tempInst.sub.val = "bar";
    vec.push_back( tempInst );


    // 1000 lines of random code here

    int i;
    int size = vec.size( );
    myMainContainer current;

    for( i = 0; i < size; i ++ )
    {
        cout << i << ": Value before='" << vec.at( i ).sub.val << "'" << endl;

        current = vec.at( i );
        doSomethingWith( current );

        cout << i << ": Value after='" << vec.at( i ).sub.val << "'" << endl;
    }
    system("pause");//i suck

}

A hell lot of code for an example, I know.

Now so you don’t have to spend years thinking about what this [should] do[es]: I have a class myMainContainer which has as its only member an instance of mySubContainer. mySubContainer only has a string val as member.

So I create a vector and fill it with some sample data.

Now, what I want to do is: Iterate through the vector and make a separate function able to modify the current myMainContainer in the vector. However, the vector remains unchanged as the output tells:

0: Value before='foo'
0: Value after='foo'
1: Value before='bar'
1: Value after='bar'
  • What am I doing wrong?

doSomethingWith has to return void, I can’t let it return the modified myMainContainer and then just overwrite it in the vector, that’s why I tried to pass it by reference as seen in the doSomethingWith definition above.

  • 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-14T05:23:55+00:00Added an answer on May 14, 2026 at 5:23 am

    You’re making a copy of the vector here:

    current = vec.at( i );
    

    and modifying current, but printing the original, vec.at(i).

    Instead, modify the object directly, e.g.

    doSomethingWith(vec[i]);  // or vec.at(i) for checked access.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Escape it with another %: $stringWithVariables = 'About to deduct… May 16, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer I have an idea after a while: You can pin… May 16, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer Ok so I went through the wiki and saw that… May 16, 2026 at 5:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Before I ask my question, please take a look at this example function: DateTime.TryParse(01/01/2000,
Please take a look at this spider example in Scrapy documentation. The explanation is:
Please take a look at this site as a reference: http://www.philsalesses.com/place-pulse/ On the left
Please take a look at this navigation: http://beta.viewcms.info/mint/ And you can see the hover
could you take a look at this http://jsbin.com/osolo/ please? If you enter a letter
please take a look at the blue menu bar at the top of http://www.animefushigi.com
please take a look at the following website: http://www.solidcamxpress.co.uk/ If you look at it
Can someone please explain to me what is the difference between protected / public
I wrote a regex, but it doesn't work as i expect. Take a look
my jQuery skills are pretty good normally but this is driving me mad! It's

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.