不能将sregex_iterator作为流传递给 cout
Cannot pass sregex_iterator as a stream to cout
我将以下文件保存为第一个.cpp
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main (){
regex filenameRegex("[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z0-9]+");
string s2 = "Filenames are readme.txt and my.cmd.";
sregex_iterator it(s2.begin(), s2.end(), filenameRegex);
sregex_iterator it_end;
while(it != it_end){
cout << (*it) << endl;
++it;
}
return 0;
}
尝试首先使用命令 g++ 编译它.cpp -o first -std=c++11 会产生以下错误:
first.cpp: In function ‘int main()’:
first.cpp:16:15: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
cout << (*it) << endl;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from first.cpp:1:
/usr/include/c++/4.9/ostream:602:5: note: 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::match_results<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
我对这里发生的事情感到非常困惑。此处的教程指出,顺从sregex_iterator对象应返回一个字符串。这不是真的吗?
std::sregex_iterator
是 std::match_results<std::string::const_iterator>
上的迭代器。要从中获得std::string
,您需要使用 str()
方法:
while(it != it_end){
std::cout << it->str() << 'n';
++it;
}
顺便说一句,不要使用std::endl
.
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 在 COUT 语句中使用 COUT 调用函数
- GCC 4.8.2 自动矢量化由于 cout 而失败
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- C++ Setter/Getter,cout 工作,printf 失败
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 结构元素名称要 cout?
- 使用模板类重载 cout
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- 带有 cout 的指针在C++
- cout 指针时的实际数字
- COUT 无法提供输出可能是由于内联组装
- C++程序在一定行后未正确编译[cout语句]
- 如何在逗号后使用 cout 打印整数值,而无需在逗号后添加额外的零C++?