迭代字符串集时没有可行的转换
No viable conversion when iterating over string set
给定集合:
{"警报", "警报", "问题"}
我想要输出:
alarm
alert
issue
我想向后或向前打印集合的每个元素。以下是我迄今为止尝试的代码:
set<string> str_set = {"hi", "bye", "there"}; string cur_word;
for(long iter = str_set.begin(); iter!= set.end(); iter++) {
cout << iter << endl;
}
我收到错误:
No viable conversion from 'std::__1::set<std::__1::basic_string<char>,
std::__1::less<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::basic_string<char> > >::iterator' (aka
'__tree_const_iterator<std::__1::basic_string<char>,
std::__1::__tree_node<std::__1::basic_string<char>, void *> *, long>') to
'long'
代码的问题在于std::set<std::string>::begin()
返回的迭代器正在转换为long
时间。 应将iter
类型定义为std::set<std::string>::iterator
或auto
。
std::set<std::string> str_set = {"hi", "bye", "there"};
for(auto iter = str_set.begin(); iter != str_set.end(); iter++) {
std::cout << *iter << 'n';
}
auto
相当于std::set<std::string>::iterator
.
或
std::set<std::string> str_set = {"hi", "bye", "there"};
for(auto &value : str_set) {
std::cout << value << 'n';
}
auto
相当于std::string&
.
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数