基于循环的C ISTRINGSTREAM范围没有开始成员
C++ istringstream range-based for loop has no begin member
我正在尝试为用户输入的特殊字符进行基本的解析。这篇文章显示了如何在Whitespace上拆分,但是当我尝试将字符串存储到字符串的向量中时,我会遇到此编译错误。
repl.cpp: In function ‘int main(int, char**)’:
repl.cpp:52:25: error: range-based ‘for’ expression of type ‘std::__cxx11::basic_istringstream<char>’ has an ‘end’ member but not a ‘begin’
for (string s : iss)
^~~
repl.cpp:52:25: error: ‘std::ios_base::end’ cannot be used as a function
make: *** [repl.o] Error 1
这是下面的完整代码:
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
using namespace std;
int main(int argc, char *argv[])
{
size_t pos;
int pipe = 0;
int pid = 0;
vector <size_t> positions;
vector <string> arguments;
do
{
cout << "repl$ ";
getline(cin, cmd);
pos = cmd.find( "|", 0);
while ( pos != string::npos )
{
positions.push_back(pos);
pos = cmd.find( "|", pos+1);
pipe += 1;
pid += 1;
}
istringstream iss(cmd);
while (iss >> cmd)
arguments.push_back(cmd);
for (string s : iss)
cout << s << endl;
} while (cmd != "q");
return EXIT_SUCCESS;
}
您需要使用std::istream_iterator<std::string>
读取连续字符串。Boost有一个包装器来创建一个伪容器,代表istream
读取的对象的顺序;例如:
for (const auto& s : boost::range::istream_range<std::string>(iss))
std::cout << s << 'n';
在这种特定情况下的一种替代方案是直接复制到输出迭代器:
std::copy(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{},
std::ostream_iterator<std::string>{std::cout, 'n'});
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 如何访问超出其块范围的对象?
- 基于循环的C ISTRINGSTREAM范围没有开始成员