0

I have an std::vector of objects. For each object in the vector, I want to modify the value of an object member. The following thoughts came into my mind:

  1. If I had a vector of primitives, I would use std::fill to do that. However, I cannot imagine how to apply std::fill to a vector of objects.

  2. I know that I could definitely iterate over all objects using an iterator but that is much code for a tiny task.

What is the best way to modify the value of an object member for all objects in std::vector?

Finnfalter
  • 732
  • 2
  • 7
  • 22

6 Answers6

3

I think you can use lambda to handle this problem; just like this:

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

int main()
{
    int temp = 10;
    vector<int> ivec = {30, -10, -20, 50, 40 ,100, -50};
    std::for_each(ivec.begin(), ivec.end(), [&](int &x) { x += temp; cout << x << endl;});
    return 0;
}

but only c11 support lambda,you must make sure your complier support c11!

minicaptain
  • 1,196
  • 9
  • 16
  • 1
    thank You, I applied the for_each (+1). Mmhh..when I checked my compiler for C++11 support recently, as done in [here](http://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support), it showed a 'pre-C++11' version. However, it works and looks concise to me. – Finnfalter Dec 12 '13 at 09:28
3

If you want to avoid the for loop with iterators, you have various ways.

std::for_each with functor structure:

 struct Functor
 { 
    Functor(int new_value)
    { 
        _new_value = new_value;
    }

    void operator()(Object& object)
    {
        object.modify = _new_value;
    }

    int   _new_value;
 };

std::for_each(objects.begin(), objects.end(), Functor(1));

std::for_each with function:

  void function_modify(Object& object)
  {
      object.modify = 2;
  }

  std::for_each(objects.begin(), objects.end(), function_modify);

std::for_each with lambda:

 std::for_each(objects.begin(), objects.end(), [](Object& object) { object.modify = 3; } );

c++11 for loop:

for (auto& object: objects) { object.modify = 4; }

http://ideone.com/n1SNnL

Johan
  • 3,728
  • 16
  • 25
2

I may have not well understand your needs, but if you want to affect a value for each same field of your vector elements, here are some methods :

struct MyStruct
{
    int a;
    char b;
};


int main(int argc, char const *argv[])
{
    std::vector<MyStruct> myVector;

    //... Fill the vector

    // First way
    for(std::vector<MyStruct>::iterator vIt = myVector.begin() ; vIt != myVector.end() ; ++vIt)
        vIt->a = 42;

    // C++11
    for(auto vIt = myVector.begin() ; vIt != myVector.end() ; ++vIt)
        vIt->a = 42;

    // Also C++11
    for(MyStruct& vElement : myVector)
        vElement.a = 42;

    // Even more C++11
    for(auto& vElement : myVector)
        vElement.a = 42;

    return 0;
}
Oragon Efreet
  • 1,114
  • 1
  • 8
  • 25
  • There are even more if you count in the `std::algorithm` solutions like `transform` and `for_each` with lambda. – RedX Dec 12 '13 at 09:20
  • Thanks Oragon Efreet (+1), the third example looks pretty concise to me. If I had C++11 available in my project, I would have used it! – Finnfalter Dec 12 '13 at 09:25
1

Sadly all i can think of is to iterate over the objects. If you don't want to use an iterator you could give for_each a go.

Smurker
  • 714
  • 6
  • 17
1

For example

std::vector<Object> v( 10, Object() );

for ( Object &obj : v ) obj.member = value;

or

std::vector<Object> v( 10, Object() );

std::for_each( v.begin(), v.end(), []( Object &obj ) { obj.member = value; } );

or

Object obj;
obj.member = value;

std::vector<Object> v( 10, obj );

or

std::vector<Object> v;

Object obj;
obj.member = value;

v.assign( 10, obj );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If looping is not preferable, you can do this

class myClass
{
    int a; // Variable to be modified

    public:

    myClass (const myClass&t)
    {
       a = 5;// assumed value to be set
    }

    myClass& operator=()
    {
        a = 5;// assumed value to be set
        return *this;
    }

    myClass& operator*()
    {
       a= 5; // assumed value to be set
       return *this;
    }
};

This will make sure that the value is set before the object is used, and thereby partly solve your problem.

Saran-san
  • 361
  • 3
  • 14