gpt4 book ai didi

C++ 循环 std::vector>

转载 作者:行者123 更新时间:2023-11-27 23:00:13 29 4
gpt4 key购买 nike

如何循环?

我已经试过了:

//----- code
std::vector<std::map<std::string, std::string> >::iterator it;
for ( it = users.begin(); it != users.end(); it++ ) {
std::cout << *it << std::endl; // this is the only part i changed according to the codes below
}
//----- error
error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::map<std::basic_string<char>, std::basic_string<char> >]’

//----- code
std::cout << *it["username"] << std::endl;
//----- error
note: template argument deduction/substitution failed:
note: ‘std::map<std::basic_string<char>, std::basic_string<char> >’ is not derived from ‘const std::complex<_Tp>’
//----- code
std::cout << *it->second << std::endl; // also tried with parenthesis - second()
//----- error
error: ‘class std::map<std::basic_string<char>, std::basic_string<char> >’ has no member named ‘second’
//----- code
for( const auto& curr : it ) std::cout << curr.first() << " = " << curr.second() << std::endl;
//----- error
error: unable to deduce ‘const auto&’ from ‘<expression error>’

最后

//----- code
std::map<std::string, std::string>::iterator curr, end;
for(curr = it.begin(), end = it.end(); curr != end; ++curr) {
std::cout << curr->first << " = " << curr->second << std::endl;
}
//----- error
‘std::vector<std::map<std::basic_string<char>, std::basic_string<char> > >::iterator’ has no member named ‘begin‘ & ‘end’

我希望我给出一个清楚的细节..上面是代码然后下面是错误..目前我的脑子一片空白。

对此感到抱歉..

我已经让它在这种类型上工作了:std::map<int, std::map<std::string, std::string> >我正在尝试使用 vector 作为选项。

最佳答案

您的迭代代码是正确的;问题是你的输出语句。您的代码是这样做的:

std::cout << *it << std::endl;

在这种情况下,*it指的是 std::map<string,string>std::cout不知道如何输出 map 。也许你想要这样的东西:

std::cout << (*it)["username"] << std::endl;

确保在 *it 周围使用 ()否则您将遇到运算符优先级问题。

关于C++ 循环 std::vector<std::map<std::string, std::string>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470087/

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