modify the value in pair<>
1 2 3 4 5
|
vector< pair<string, double> > var_map;
...here pushes pairs into var_map...
var_map.back().second = 4.3;
| |
why can the last line not modify the value stored in the last element of
var_map
?
What is the right way to do that?
why can the last line not modify the value stored in the last element of var_map? |
It ought to.
What is the right way to do that? |
Looks ok to me, as long as the vector isn't empty.
This works for me:
1 2 3 4 5 6 7 8 9 10 11
|
#include <string>
#include <utility>
#include <vector>
#include <iostream>
int main() {
std::vector< std::pair< std::string, double > > var_map;
var_map.push_back( std::make_pair( std::string( "Hello" ), 3.14 ) );
var_map.back().second = 1.414;
std::cout << var_map.back().first << " -> " << var_map.back().second << std::endl;
}
| |
Oh! I made a mistake somewhere else. :-(
Topic archived. No new replies allowed.