gpt4 book ai didi

c++ - 迭代器的值

转载 作者:搜寻专家 更新时间:2023-10-31 00:05:36 24 4
gpt4 key购买 nike

我创建了一张 map 。我想使用 map 中的 itr 将 key 的索引打印到文件中。这就是我的意思:

map <string,int> VendorList;  
VendorList[abc] = 0;
VendorList[mazda] = 111;
VendorList[ford] = 222;
VendorList[zoo] = 444;
map <string,int>::iterator itr=VendorList.find("ford");
fstream textfile;
textfile << itr;

如果我在查找行 abc 中输入我希望程序计算出 1。

如果我输入查找行 mazda,我希望程序 cout 2。

如果我输入查找行 ford,我希望程序计算 3。

如果我放入 find line zoo 我希望程序计算出 4。

我该怎么做?编译器在线上大喊:

 textfile << itr;

它给出了这个错误:错误 C2679:二进制“<<”:未找到接受类型为“std::_Tree<_Traits>::iterator”的右手操作数的运算符(或没有可接受的转换)

最佳答案

你的程序有很多错误。坦率地说,我不确定您的要求。但无论如何试试这个:

map <string,int> VendorList;  
VendorList["abc"] = 1;
VendorList["mazda"] = 2;
VendorList["ford"] = 3;
VendorList["zoo"] = 4;
map <string,int>::iterator itr=VendorList.find("ford");
cout<<(itr->second);// this will print 3

编辑:
另外正如有人建议使用成对 vector ,我认为他是对的。尝试这样的事情。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
typedef vector<pair<string,int> > Vm;
Vm V;
V.push_back(make_pair("abc",0));
V.push_back(make_pair("mazda",111));
V.push_back(make_pair("ford",222));
V.push_back(make_pair("zoo",444));

for(size_t i=0;i!=V.size();++i)
if(V[i].first=="ford")
cout<<(i+1);
}

根据需要修改上述程序。
希望对您有所帮助。

关于c++ - 迭代器的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1965461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com