class MotionItem//
{
public:
int srcPatch;
int srcid;
int destPatch;
int destid;
MotionItem(int,int,int,int);
};
int main(){
MotionItem a1(1,0,2,0);
MotionItem a2(2,3,4,3);
vector<const MotionItem *> v;
v.push_back(&a1);
v.push_back(&a2);
vector<const MotionItem*> ::iterator it;
for (it = v.begin();it != v.end(); it++)
cout << (*it)->srcPatch << endl; //vector的访问是没问题的
// 怎么通过map键值对的方式找到vector的地址,再访问vector里面的内容呢?
map<int,vector<const MotionItem *>* > m;
m.insert(pair<int ,vector<const MotionItem*> *> (1,&v));
cout << *(m.find(1))-> << endl; //这局想通过map找到容器里面的类对象 写不下去了 求高手啊~~跪求
return 0;
}
{
public:
int srcPatch;
int srcid;
int destPatch;
int destid;
MotionItem(int,int,int,int);
};
int main(){
MotionItem a1(1,0,2,0);
MotionItem a2(2,3,4,3);
vector<const MotionItem *> v;
v.push_back(&a1);
v.push_back(&a2);
vector<const MotionItem*> ::iterator it;
for (it = v.begin();it != v.end(); it++)
cout << (*it)->srcPatch << endl; //vector的访问是没问题的
// 怎么通过map键值对的方式找到vector的地址,再访问vector里面的内容呢?
map<int,vector<const MotionItem *>* > m;
m.insert(pair<int ,vector<const MotionItem*> *> (1,&v));
cout << *(m.find(1))-> << endl; //这局想通过map找到容器里面的类对象 写不下去了 求高手啊~~跪求
return 0;
}
解决方案
60
cout << (*(m[1]))[0]->srcPatch << endl;
m[1]通过key=1取得vector<const MotionItem*> *类型
*(m[1])得到vector<const MotionItem*>类型
(*(m[1]))[0]得到vector中的第一个元素,是const MotionItem*类型
(*(m[1]))[0]->srcPatch取得第一个元素的srcPatch值
10
#include <vector> #include <map> #include <iostream> using namespace std; class MotionItem// { public: int srcPatch; int srcid; int destPatch; int destid; MotionItem(int,int,int,int); }; int main(){ MotionItem a1(1,0,2,0); MotionItem a2(2,3,4,3); vector<const MotionItem *> v; v.push_back(&a1); v.push_back(&a2); vector<const MotionItem*> ::iterator it; for (it = v.begin();it != v.end(); it++) cout << (*it)->srcPatch << endl; //vector的访问是没问题的 // 怎么通过map键值对的方式找到vector的地址,再访问vector里面的内容呢? map<int,vector<const MotionItem *>* > m; m.insert(pair<int ,vector<const MotionItem*> *> (1,&v)); vector<const MotionItem *> *pv; pv= m.find(1); vector<const MotionItem*> ::iterator it; for (it = pv->begin();it != pv->end(); it++) cout << (*it)->srcPatch << endl; return 0; }