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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:31:47+00:00 2026-06-17T14:31:47+00:00

I have defined a vector which consist of pairs and vectors. For better readability,

  • 0

I have defined a vector which consist of pairs and vectors. For better readability, I do this

#include <iostream>
#include <vector>
using namespace std;
class foo {
public:
  vector< pair<int, vector<int> > > v;
  vector< pair<int, vector<int> > >::iterator it;
  void bar() 
  {
    for( it = v.begin(); it != v.end(); it++ ) {
      if ( 10 == (*it).first ) {
          vector<int> a = (*it).second;   // NOTE
          a[0] = a[0]*2;                  // NOTE
      }
    }
  }
};

int main()
{
  foo f;
  f.bar();
  return 0;
}

As you can see, I assign (*it).second to a variable a so that I manipulate easily. Problem is, when I change the value of a, the original vector v doesn’t change. I can resolve that changing a to (*it).second every where in the loop. However this will make the code hard for reading.

Is there any way to reflect the changes in a to the original v? I have to say call by reference like this

vector<int> a = &(*it).second;

doesn’t work

  • 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-17T14:31:48+00:00Added an answer on June 17, 2026 at 2:31 pm

    You need to declare a as a reference:

    vector<int>& a = (*it).second;

    Ampersand in std::vector<int> a = &(*it).second; is being parsed as adress-of operator and a isn’t a pointer – that’s why it doesn’t work/compile.

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

Sidebar

Related Questions

I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have
I have an object defined as : std::unordered_map<std::string, std::vector<int>> large_obj; It can store very
Suppose I have an object which is defined as an array of vector points,
I have a large function which has a vector defined as follows: v <-
I have a vector class and I defined the __mul__ method to multiply a
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have defined a rectangle drawable like this: <shape xmlns:android=http://schemas.android.com/apk/res/android android:shape=rectangle> <corners android:radius=4dip/> <solid
I have defined a class in C++ which holds an array of scalars of
I'm trying to write a class which contains several std::vectors as data members, and

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.