Josh Posted May 26, 2011 Share Posted May 26, 2011 I've never used the std::multimap before, and have use for it now. Here's the class: class Actor : public Object { public: std::multimap<std::string,Output> outputs; }; I need to add an object into the map, without removing other objects that use the same key: void Actor::AddOutput(Actor* target,const std::string& outputname, const std::string& inputname) { Output out; out.target = target; out.functionname = inputname; outputs.insert(pair<std::string,Output>(outputname,out));???????????? } And I want to iterate through all objects for a given key: void Actor::CallOutputs(const std::string& name) { /*Output out; out = (outputs.find(name))->Second out.Call();???????*/ } Anyone know how to do this? Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Canardia Posted May 26, 2011 Share Posted May 26, 2011 #include <string.h> #include <iostream> #include <map> #include <utility> using namespace std; int main() { // Compare (<) function not required since it is built into string class. // else declaration would comparison function in multimap definition. // i.e. multimap<string, int, compare> m; multimap<string, int> m; m.insert(pair<string, int>("a", 1)); m.insert(pair<string, int>("c", 2)); m.insert(pair<string, int>("b", 3)); m.insert(pair<string, int>("b", 4)); m.insert(pair<string, int>("a", 5)); m.insert(pair<string, int>("b", 6)); cout << "Number of elements with key a: " << m.count("a") << endl; cout << "Number of elements with key b: " << m.count("b") << endl; cout << "Number of elements with key c: " << m.count("c") << endl; cout << "Elements in m: " << endl; for (multimap<string, int>::iterator it = m.begin(); it != m.end(); ++it) { cout << " [" << (*it).first << ", " << (*it).second << "]" << endl; } pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp; // equal_range( returns pair<iterator,iterator> representing the range // of element with key b ppp = m.equal_range("b"); // Loop through range of maps of key "b" cout << endl << "Range of \"b\" elements:" << endl; for (multimap<string, int>::iterator it2 = ppp.first; it2 != ppp.second; ++it2) { cout << " [" << (*it2).first << ", " << (*it2).second << "]" << endl; } // Can't do this (??) // cout << ppp.first << endl; // cout << ppp.second << endl; m.clear(); } Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.