Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a vector item in C++?

Tags:

c++

copy

vector

I've got a vector of structs in C++ and I would like to modify each item individually. I found that doing SomeStruct info = myVector[i] gives me a copy of the item, so if I modify it nothing will be changed. So right now I'm resetting the item like that: myVector[i] = info. Is there a more efficient way do that? One that won't involve a copy operation?

This is my current code:

struct CharacterInfo {
    QChar character;
    int occurrences;
    double frequency;
};

std::vector<CharacterInfo> characterInfos;

// Some code to populate the vector

for (unsigned i = 0; i < characterInfos.size(); i++) {
    CharacterInfo info = characterInfos[i];
    info.frequency = (double)info.occurrences / (double)totalOccurrences;
    characterInfos[i] = info; // how to avoid this?
}
like image 351
laurent Avatar asked Nov 23 '11 07:11

laurent


People also ask

How do you change a vector element?

To replace an element in Java Vector, set() method of java. util. Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element.

How do you modify a vector?

How can I edit vector files? Vector images are often saved as AI files to enable quick editing. These files are editable in Adobe Illustrator. Images saved in this format can also be converted to PDF files, allowing them to be edited in Adobe Acrobat and making them easier to print.

Can you change values of the vector?

To change the value of a vector item, we refer to their index number or position in the given vector inside brackets [] . We then assign it to a new value.

How do I change the value of a vector at a specific index?

An element in the vector can be replaced at a particular/specified index with another element using set() method, or we can say java. util. Vector. set() method.


2 Answers

The simplest way which doesn't change too much of your code is just to use a reference instead of an instance. So:

SomeStruct & info = myVector[i];

The next easiest way is to change from using a loop with an index, so like:

for (std::vector<SomeStruct>::iterator it = myVector.begin(); it != myVector.end(); ++it)
{
    SomeStruct & info = *it;
    // do stuff here
}

With the STL you can go even further, especially if you have a C++11 capable compiler, for instance:

std::for_each(std::begin(myVector), std::end(myVector), [](SomeStruct & info) { /* do stuff here */ });

Also not related to your question directly, but if you add a method to the struct that computes the frequency, the code becomes much cleaner, for instance following from the last example you could do:

std::for_each(std::begin(myVector), std::end(myVector), std::mem_fun(&SomeStruct::calculateFrequency));

This will also work without a C++11 compiler if you change the calls to std::begin(myVector) with myVector.begin() and the same for end.

like image 168
Daemin Avatar answered Oct 14 '22 08:10

Daemin


You can use a reference:

CharacterInfo& info = characterInfos[i];
info.frequency = (double)info.occurrences / (double)totalOccurrences;

The reference info is bound to the element of your vector. If you change it, you change the vector element too.

like image 45
Gunther Piez Avatar answered Oct 14 '22 06:10

Gunther Piez