C++ 如何到达迭代器';在地图上有什么价值?
这是我的密码:C++ 如何到达迭代器';在地图上有什么价值?,c++,dictionary,iterator,C++,Dictionary,Iterator,这是我的密码: map<string,pair<string,string> >::iterator last; map<pair<string,int>,pair<string,vector<int> > >::iterator location; map<pair<string,int>,pair<string,vector<int> > >::it
map<string,pair<string,string> >::iterator last;
map<pair<string,int>,pair<string,vector<int> > >::iterator location;
map<pair<string,int>,pair<string,vector<int> > >::iterator location1;
auto predicate = [&](auto& course) { return compareName(course, c); };
studentLoc = find(getStudentList().begin(),getStudentList().end(),s);
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
if(studentLoc != getStudentList().end() || location != getMatchMap().end())
{
location1 = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
if(location1 != getMatchMap().end())
getCourseScList().insert({s,{make_pair(location1->first.first,location1->second.first)}});
map::迭代器last;
迭代器位置;
迭代器位置1;
自动谓词=[&](自动和进程){返回compareName(进程,c);};
studentLoc=find(getStudentList().begin(),getStudentList().end(),s);
location=find_if(getMatchMap().begin(),getMatchMap().end(),谓词);
如果(studentLoc!=getStudentList().end()| | location!=getMatchMap().end())
{
location1=find_if(getMatchMap().begin(),getMatchMap().end(),谓词);
if(location1!=getMatchMap().end())
getCourseScList().insert({s,{make_pair(location1->first.first,location1->second.first)});
我在这里做的是,当谓词函数工作时,我将把这些数据插入到另一个映射中。
但是,当我尝试使用此代码打印此文件时:
for(last = getCourseScList().begin();last!=getCourseScList().end();++last)
{
cout<<last->first<<"\t\t\t\t"<<(last->second.first)<<"\t\t\t"<<last->second.second<<endl;
}
for(last=getcourseslist().begin();last!=getcourseslist().end();+last)
{
coutiter->second
是地图中的值…是的,我知道。但是当我将这个iter->second插入另一个地图时,我看不到其中的任何值。你说“看到任何值”是什么意思?如果你在另一个地图中插入值的副本,它将在另一个地图中。没有实际的(完整的,可编译的)演示您尝试执行的操作的代码,没有人能给您提供比这更好的帮助。使用for循环,我必须查看映射的值,对吗?但是编译器不会在屏幕上打印任何内容。我也会添加整个代码。猜测一下getCourseScList()
会按值返回映射,因此您有未定义的行为。请发布一个
void Schedule::studentSchedule()
{
string c,s;
int courseNum;
int i = 0;
list<string>::iterator studentLoc;
map<string,pair<string,string> >::iterator last;
map<pair<string,int>,pair<string,vector<int> > >::iterator location;
map<pair<string,int>,pair<string,vector<int> > >::iterator location1;
dummy1:
cout<<"Enter the student name"<<endl;
cin >> s;
cout<<"Enter the course names you want"<<endl;
cin>>c;
auto predicate = [&](auto& course) { return compareName(course, c); };
studentLoc = find(getStudentList().begin(),getStudentList().end(),s);
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
if(studentLoc != getStudentList().end() || location != getMatchMap().end())
{
location1 = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
if(location1 != getMatchMap().end())
getCourseScList().insert({s,{make_pair(location1->first.first,location1->second.first)}});
}
else
{
cout<<"The course or student you wrote isn't available.Please enter existed courses!"<<endl;
goto dummy1;
}
cout<<"\n"<<endl;
char dummy;
cout<<"\nDo tou want to create a new schedule?Press y or n."<<endl;
cin>>dummy;
if(dummy == 'Y' || dummy == 'y')
goto dummy1;
else
{
for(last = getCourseScList().begin();last!=getCourseScList().end();++last)
{
cout<<last->first<<"\t\t\t\t"<<(last->second.first)<<"\t\t\t"<<last->second.second<<endl;
}
}
}