How to change a vector element in C ++?

I have a vector of structures in C ++, and I would like to change each element individually. I found that doing SomeStruct info = myVector[i] gives me a copy of the item, so if I change it, nothing will change. So now I am rewriting the element as follows: myVector[i] = info . Is there a more efficient way? One that will not include 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? } 
+4
source share
4 answers

The easiest way that doesn't change your code too much is to simply use the link instead of the instance. So:

 SomeStruct & info = myVector[i]; 

The next simple way is to switch from using a loop with an index, so that:

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

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

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

Also not directly related to your question, but if you add a method to a structure that calculates the frequency, the code becomes much cleaner, for example, following 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 the C ++ 11 compiler if you change the calls to std::begin(myVector) to myVector.begin() and the same for the end.

+12
source

You can use the link:

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

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

+8
source

You can iterate through a vector using the STL iterator:

 for (vector<CharacterInfo>::iterator it = characterInfos.begin(); it != characterInfos.end(); ++it) { it->frequency = (double)it->occurrences / totalOccurrences; } 

In the loop, it is an iterator that has basically the same functionality and interface as the pointer to the CharacterInfo structure: http://cplusplus.com/reference/std/iterator/RandomAccessIterator/

Quoting with an iterator is a more idiomatic way of repeating each element from std::vector , unless you need to know the index of each element.

+3
source

I'm not sure I understand your question, but I think you are trying to do this?

 for (unsigned i = 0; i < characterInfos.size(); i++) { characterInfos[i].frequency = (double)characterInfos[i].occurrences / (double)totalOccurrences; } 

Another option is to use iterators:

 for(std::vector<CharacterInfo>::iterator it = characterInfos.begin(); it != characterInfos.end(); ++it){ it->frequency = (double)it->occurences / (double)totalOccurences; } 
+1
source

Source: https://habr.com/ru/post/1382651/


All Articles