Created
October 21, 2015 05:10
-
-
Save xunkai55/ef961e2c390b0d0f1c6a to your computer and use it in GitHub Desktop.
Use a pointer to directly manipulate elements in an STL map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
using namespace std; | |
struct S{ | |
int data; | |
float f; | |
S(int d, float x): data(d), f(x) {} | |
S(): data(0), f(0) {} | |
}; | |
int main() { | |
// your code goes here | |
std::map<int, S> m; | |
m.emplace(1, S(2, 3)); | |
m.emplace(3, S(4, 5)); | |
S *s = &m.at(3); | |
s->f = 6; | |
cout << m[3].f << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment