64,649
社区成员
发帖
与我相关
我的任务
分享
#include <boost/shared_ptr.hpp>
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Test
{
private:
map<string, shared_ptr<map<string, string>>> m_Map;
public:
void test()
{
shared_ptr<map<string, string>> mapPtr1 = shared_ptr<map<string, string>>(new map<string, string>);
mapPtr1->insert(make_pair("1", "a"));
shared_ptr<map<string, string>> mapPtr2 = shared_ptr<map<string, string>>(new map<string, string>);
mapPtr2->insert(make_pair("2", "b"));
m_Map.insert(make_pair("IM", mapPtr1));
m_Map.insert(make_pair("HTTP", mapPtr2));
}
map<string, shared_ptr<map<string, string>>> getMap()
{
return m_Map;
}
};
int main(int argc, char *argv[])
{
Test obj;
obj.test();
map<string, shared_ptr<map<string, string>>>::iterator iter = obj.getMap().find("IM");
cout << iter->first << endl;
return 0;
}
map<string, shared_ptr<map<string, string>>> MAP = obj.getMap();
map<string, shared_ptr<map<string, string>>>::iterator iter = MAP.find("IM");
cout << iter->first << endl;
Iterator definitions
An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators).
The most obvious form of iterator is a pointer: A pointer can point to elements in an array, and can iterate through them using the increment operator (++). But other kinds of iterators are possible. For example, each container type (such as a list) has a specific iterator type designed to iterate through its elements.
Notice that while a pointer is a form of iterator, not all iterators have the same functionality of pointers; Depending on the properties supported by iterators, they are classified into five different categories:
msdn上的。map<string, shared_ptr<map<string, string>>>& getMap()
{
return m_Map;
}
返回引用
cout << (obj.getMap().find("IM"))->first << endl;
直接这样也是可以输出的,和上面又差在什么地方呢,希望谁能给解答一下,谢谢。