错误:与“运算符”不匹配
我想同一个键和多个值,例如键是10值是2,3,4,但“*iter”是错误的..如何用c语言计算map,vector?
您可以定义如何通过< code>std::ostream打印内容,如下所示:
#include <iostream>
#include <map>
#include <vector>
#include <string>
// define how to print std::pair<std::string, std::vector<int>>
std::ostream& operator<<(std::ostream& stream, const std::pair<std::string, std::vector<int>>& pair) {
stream << "(" << pair.first << ", {";
bool first = true;
for (int e : pair.second) {
if (!first) stream << ", ";
stream << e;
first = false;
}
stream << "})";
return stream;
}
int main(void) {
std::string yytext = "hoge";
int lineno = 42;
// below is copied from the question
std::map<std::string,std::vector<int>> m;
m[yytext].push_back(lineno);
std::map<std::string,std::vector<int>>::iterator iter;
for (iter=m.begin(); iter!=m.end(); iter++){
std::cout<<iter->first<<":"<<*iter<<std::endl;}
}
我建议使用结构化绑定和基于范围的for循环:
std::map<std::string,std::vector<int>> m;
for (auto&[str, vec] : m) { // bind str to "first" in the pair and vec to "second"
std::cout << str << ':';
for(auto lineno : vec) std::cout << ' ' << lineno;
std::cout << '\n';
}
在代码片段中,表达式*iter
的值是类型为std::对的对象
以及错误消息
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and
‘std::pair<const std::__cxx11::basic_string, std::vector >’)
说的是这个。
最简单的方法是使用基于范围的for循环。
这是一个演示程序。
#include <iostream>
#include <string>
#include <vector>
#include <map>
int main()
{
std::map<std::string, std::vector<int>> m;
m["10"].assign( { 2, 3, 4 } );
for ( const auto &p : m )
{
std::cout << p.first << ": ";
for ( const auto &item : p.second )
{
std::cout << item << ' ';
}
std::cout << '\n';
}
return 0;
}
程序输出为
10: 2 3 4
如果要使用迭代器编写普通的 for 循环,则循环可以如下所示。
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iterator>
int main()
{
std::map<std::string, std::vector<int>> m;
m["10"].assign( { 2, 3, 4 } );
for ( auto outer_it = std::begin( m ); outer_it != std::end( m ); ++outer_it )
{
std::cout << outer_it->first << ": ";
for ( auto inner_it = std::begin( outer_it->second );
inner_it != std::end( outer_it->second );
++inner_it )
{
std::cout << *inner_it << ' ';
}
std::cout << '\n';
}
return 0;
}
同样,程序输出是
10: 2 3 4
我在制作一个向量时遇到了麻烦,它会返回向量的原始元素平方。 这是我得到的错误。
本文向大家介绍C ++ STL中的map :: at()和map :: swap(),包括了C ++ STL中的map :: at()和map :: swap()的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将讨论C ++ STL中map::at()和map::swap()函数的工作,语法和示例。 什么是C ++ STL中的映射? 映射是关联容器,它有助于按特定顺序存储由键值和映射值的组
本文向大家介绍C ++ STL中的map :: at(),包括了C ++ STL中的map :: at()的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将讨论C ++ STL中map::at()函数的工作,语法和示例。 什么是C ++ STL中的映射? 映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值
本文向大家介绍C ++ STL中的map :: size(),包括了C ++ STL中的map :: size()的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将讨论C ++ STL中map::size()函数的工作原理,语法和示例。 什么是C ++ STL中的映射? 映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序
本文向大家介绍C ++ STL中的map :: empty(),包括了C ++ STL中的map :: empty()的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将讨论C ++ STL中map::empty()函数的工作,语法和示例。 什么是C ++ STL中的映射? 映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排
本文向大家介绍C ++ STL中的map operator =,包括了C ++ STL中的map operator =的使用技巧和注意事项,需要的朋友参考一下 在本文中,我们将讨论C ++ STL中映射等于'='运算符的工作,语法和示例。 什么是C ++ STL中的映射? 映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射