以下代码片段旨在尝试使用 stringstream 对象从字符串中提取整数,并检测整数提取是否成功。stringstream 类继承 >> 运算符以返回对 istream 实例的引用。整数提取失败如何导致 myStream 等于 0 而其 str 成员仍然是 strInput?
stringstream myStream(strInput);
if (myStream >> num){//successfull integer extraction}
else{//unsuccessfull integer extraction
cout<<myStream<<endl;
cout<<myStream.str().c_str()<<endl;}
Run Code Online (Sandbox Code Playgroud) 我必须使用一些 SWI-Prolog 代码来打开一个新流(它在文件系统上创建一个文件)并倒入一些数据。生成的文件稍后在代码中的其他地方读取。
我想用 Prolog 中的字符串流替换文件流,以便不创建文件,然后将放入流中的所有内容作为一个大字符串读取。
SWI-Prolog 有字符串流吗?如果是这样,我如何使用它们来完成此任务?如果您能提供一个小片段,我将不胜感激。谢谢!
我有一个字符串:
string line="6,148,72,35,0,33.6,0.627,50,1";
Run Code Online (Sandbox Code Playgroud)
我想将其中的数字复制到std :: vector的各个元素中.我想使用std :: vector.assign()或std :: copy().
所以我写道:
string line="6,148,72,35,0,33.6,0.627,50,1";
vector<double> row;
istringstream iss { line };
row.assign(istream_iterator<double>(iss), istream_iterator<double>());
Run Code Online (Sandbox Code Playgroud)
但结果是assign()只将字符串中的第一个数字复制到向量,即得到
row={6}
Run Code Online (Sandbox Code Playgroud)
虽然我想得到
row={6,148,72,35,0,33.6,0.627,50,1}
Run Code Online (Sandbox Code Playgroud)
如果我使用std :: copy,就像在:
string line="6,148,72,35,0,33.6,0.627,50,1";
vector<double> row;
istringstream iss { line };
copy(istream_iterator<double>(iss), istream_iterator<double>(), back_inserter(row));
Run Code Online (Sandbox Code Playgroud)
看完第一个数字后,看起来字符串的副本结束,我不知道为什么.知道如何使用assign()或copy()将字符串中的每个数字复制到向量中吗?
我会直接去 MCVE:
#include <sstream>
struct A
{
inline static std::stringstream ss;
};
Run Code Online (Sandbox Code Playgroud)
GCC 7.2 和 7.1拒绝编译它并出现以下错误:
错误:没有用于调用“std::__cxx11::basic_stringstream::basic_stringstream()”的匹配函数
内联静态 std::stringstream ss;
^~
在 blah:1:0 包含的文件中:
/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/sstream:723:7:注意:候选:std::__cxx11::basic_stringstream::basic_stringstream(std::__cxx11::basic_stringstream&&) [与_CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
基本字符串流(基本字符串流&& __rhs)
^~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/sstream:723:7: 注意:候选需要 1 个参数,提供 0
您可以在没有任何标志的情况下重现它,也可以使用-std=c++17.
Clang 5.0 编译它没有任何问题。
其他类(例如std::string)可以inline static毫无问题地制作。
使其成为非内联静态消除错误。
不是 GCC 错误还是我遗漏了什么?
程序在逗号之间找到整数,如“2,33,5”-> 2 33 5。问题是如果我把例如“0,12,4”这样的字符串,它为什么工作。stringstream 不应该将 0 放入 tmp 中,所以循环就像开始时的 while(0) 一样吗?
vector<int> parseInts(string str) {
stringstream ss(str); //getting string
vector<int> result;
char ch;
int tmp;
while(ss >> tmp) { //while(IS IT INTEGER ALREADY OR NOT?)
result.push_back(tmp);
ss >> ch;
}
return result;
Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个项目,我想使用现代 cpp 而不是依赖旧的 c 来读取文件。对于上下文,我正在尝试读取波前 obj 文件。
我有这个旧的代码片段:
const char *line;
float x, y, z;
if(sscanf(line, "vn %f %f %f", &x, &y, &z) != 3)
break; // quitting loop because couldn't scan line correctly
Run Code Online (Sandbox Code Playgroud)
我已经翻译成:
string line;
string word;
stringstream ss(line);
float x, y, z;
if (!(ss >> word >> x >> y >> z)) // "vn x y z"
break; // quitting loop because couldn't scan line correctly
Run Code Online (Sandbox Code Playgroud)
不同之处在于我使用 astring来跳过第一个单词,但我希望它与do"vn"一样匹配sscanf。这是可能的stringstream还是我应该继续依赖sscanf …
我有一个类,它包含对stringstream的引用(用作整个应用程序日志).如何在引用的字符串流中添加文本?
一个例子(因为我不能在这里发布实际来源......)
主要
stringstream appLog;
RandomClass myClass;
.....
myClass.storeLog(&applog);
myClass.addText("Hello World");
cout << appLog.str().c_str() << endl;
Run Code Online (Sandbox Code Playgroud)
RandomClass cpp
void RandomClass::storeLog(stringstream *appLog)
{
m_refLog = appLog;
}
void RandomClass::addText(const char text[])
{
m_refLog << text; //help here...?
}
Run Code Online (Sandbox Code Playgroud)
我使用与上面非常相似的设置和方法结构在我的真实应用程序中收到以下错误.
error C2296: '<<' : illegal, left operand has type 'std::stringstream *'
error C2297: '<<' : illegal, right operand has type 'const char [11]'
我知道这个错误是因为我正在使用引用但仍然试图做"<<",但我还能怎么做呢? m_refLog-><<???
我有一个第一行显示“> FileName.txt”的文件。我的目标是阅读此行,并将“ FileName.txt”保存到一个名为name的变量中。所以我有:
ifstream file;
/* File opening stuff */
string line, name;
getline(file,line);
stringstream converter(line);
converter >> name;
Run Code Online (Sandbox Code Playgroud)
这样就完成了将“> FileName.txt”保存到变量中name,但是我需要删除'>'字符。我不确定在此之后是否应该这样做,或者是否可以使用来完全跳过它stringstream。
是否可以将char矢量转换为std::stringstream?例如:
std::vector<unsigned char> data;
std::stringstream st(????);
Run Code Online (Sandbox Code Playgroud)
有没有通过,我可以指定任何有价值的方式data来st?
我有一个stringstream我正在阅读的实例.在从流中获取数据的某个点上,我需要读取可能存在或不存在的标识符.逻辑是这样的:
std::string identifier;
sstr >> identifier;
if( identifier == "SomeKeyword" )
//process the the rest of the string stream using method 1
else
// back up to before we tried to read "identifier" and process the stream using method 2
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现上述逻辑?