c++ – How can I change the value of the elements in a vector? – Stack Overflow

I have this code, which reads in input from a file and stores it in a vector. So far, I've gotten it to give me the sum of the values within the vector and give the mean of the values using the sum. What I'd like to do now is learn how to access the vector again and subtract a value from each element of the vector and then print it out again. For example, once the sum and mean are calculated, I'd like to be able to reprint each value in the terminal minus the mean. Any suggestions/examples? You could always also devise an iterator adapter that returns the result of an operation applied to the dereference of its component iterator when it's dereferenced. Then you could just copy the vector to the output stream: You are iterating over the vector using indexes. This is not the 'STL Way' -- you should be using iterators, to wit: Point number 2 brings up another way you can improve your code. Another thing about your code that isn't very STL-ish is the use of a hand-written loop. <,algorithm>,s were designed for this purpose, and the best code is the code you never write. You can use a loop to compute the total and mean of the vector, through the use of an accumulator: If you have the benefit of a C++0x-compliant compiler, this can be made even simpler using std::for_each (within #include <,algorithm>,) and a lambda expression: You can modify the values with std::transform, though until we get lambda expressions (C++0x) it may be more trouble than it's worth: Source.

Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified:
Fatal error: Call to undefined function mdv_last_modified() in /home1/wacedu0abjm6/public_html/wp-content/themes/CherryFramework/footer.php on line 121