Finnfalter
Finnfalter

Reputation: 732

C++ best way to modify a given member of all objects in a vector of these objects

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?

Upvotes: 0

Views: 545

Answers (6)

Saran-san
Saran-san

Reputation: 360

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.

Upvotes: 0

Johan
Johan

Reputation: 3778

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

Upvotes: 3

Oragon Efreet
Oragon Efreet

Reputation: 1134

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;
}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

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 );

Upvotes: 1

minicaptain
minicaptain

Reputation: 1216

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!

Upvotes: 3

Smurker
Smurker

Reputation: 714

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.

Upvotes: 1

Related Questions