我如何从用户那里获得一个数字列表,然后将它们标记化.
这就是我所拥有的,但除了第一个数字之外它没有得到任何东西:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line = "";
cin >> line;
stringstream lineStream(line);
int i;
vector<int> values;
while (lineStream >> i)
values.push_back(i);
for(int i=0; i<values.size(); i++)
cout << values[i] << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
header.h
#include <iostream>
#include <vector>
class CombatLine{
std::stringstream Line;
std::vector<std::string> TokenLine;
void SetLine(std::string s){
Line<<s;
}
public:
void SetTokenLine(){
int i=0;
while(i<5){
Line>>TokenLine[i];
i++;}
TokenLine.resize(i);
for(int j=0;j<5;j++)
cout<<TokenLine[j];}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "Header.h"
using namespace std;
int main () {
CombatLine Line1;
Line1.SetLine("[Combat] A bird attacks -Anthrax- and misses (dodge).");
Line1.SetTokenLine();
}
Run Code Online (Sandbox Code Playgroud)
这构建但我收到此运行时错误, /cygdrive/C/Program Files/NetBeans 6.9.1/ide/bin/nativeexecution/dorun.sh: line 33: 4500 Segmentation fault <core dumped> sh "$<SHFILE>"
我知道它与我在SetTokenFile中操作字符串和流的方式有关,但我似乎无法确定是什么.
这是一个较大项目的一小部分.总的来说,我将解析一个动态文本文件,然后对整个文件的内容进行比较.
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ostringstream out;
ostringstream tmpstr;
tmpstr << "ritesh is here";
out << tmpstr.str().c_str();
out << endl;
cout << out.str();
if(tmpstr.rdbuf()!=NULL)
cout << "tmpstr not null" <<endl;
else
cout << "tmpstr null" <<endl;
delete tmpstr.rdbuf(); // This line gives me segmentation fault
cout <<"deleted" << endl;
}
Run Code Online (Sandbox Code Playgroud)
该行delete tmpstr.rdbuf(); 给出了分段错误.我猜rdbuf返回char*指针因此.我可以使用删除来释放分配给的内存空间tmpstr
我错了吗?
以下是有什么区别的
stringstream a;
stringstream b;
a << "hi";
b << "there";
Run Code Online (Sandbox Code Playgroud)
a << " " << b.rdbuf();或a << " " << b.str();或a << " " << b.str().c_str();
我一直在使用rdbuf().我注意到C++ Reference对stringstream :: rdbuf说:
请注意,对于任何成功构造的istringstream对象,即使缓冲区字符串为空,此指针也永远不会为NULL.
我认为这意味着rdbuf()不会返回NULL?我无法传递NULL.我有这样的错误char *array = NULL; a << array;.如果stringstream b为空,那么在执行时a << b.rdbuf() << "some text"是否定义了行为?在这种情况下,rdbuf返回的第一个字符是否具有eof特征,因此b中不会附加任何内容?
谢谢
我有一个带有parse(int argc, char* argv[])函数的类,我必须使用它来设置对象的所需状态.我正在使用gui中的参数stringstream,然后我试图将它们转换为char**以将它们传递给函数.这是我得到的:
std::stringstream sstream;
sstream << "-clip" << " " << min_x_entry.get_text()
<< " " << max_x_entry.get_text(); // etc.
std::cout << sstream.str(); // All looks good here
std::vector<std::string> args;
std::vector<char*> argv;
std::string arg;
while (sstream >> arg)
{
args.push_back(arg);
argv.push_back(const_cast<char*>(args.back().c_str()));
}
argv.push_back(0);
int argc = args.size();
for (int i = 0; i < argc; ++i)
std::cout << &argv[0][i]; // This outputs garbage
my_object.parse(argc, &argv[0]) // And this fails
Run Code Online (Sandbox Code Playgroud)
我错过了什么?有没有更好的方法来实现这一目标?
我稍微修改了Boost Property Tree,我遇到了这个例子.我需要将最终结果转换为vector,所以我之后又添加了一行
write_json(ss, root);
Run Code Online (Sandbox Code Playgroud)
像这样:
std::vector<char> testVec(std::begin(ss.str()), std::end(ss.str()));
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
std::string someString = ss.str()
char* someArray = (char*)someString.c_str();
std::vector<char> someVec(someArray, someArray+sizeof(someArray));
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都收到此错误:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Run Code Online (Sandbox Code Playgroud)
什么提示我做错了什么?我收集的不是属性树的问题.这是字符串到矢量转换的问题.我想这是有道理的,因为向量只应该是一维的,但我不知道如何将该字符串转换为向量并再次将其返回.
请我是编程新手.我刚开始使用c ++,我得到了一个使用stringstream的地方.这些东西让我感到困惑.请有人帮助我.
我试图将无符号字符的文件读入向量.我稍微迂回的做法是创建一个输入文件流,将该数据读入字符串流,并使用stringstream的内容初始化一个向量.可能有更好的方法来执行此操作,并且我现有的代码会导致使用某些文件出现分段错误.
string file_name = "output/comp" + to_string(start_id) + ".xml";
if( !file_exists(file_name))
{
cout << "File could not be found." << endl;
return 0;
}
ifstream ifs(file_name);
stringstream ss;
ss << ifs.rdbuf();
ss >> noskipws;
vector<unsigned char> raw_data(ss.str().begin(), ss.str().end());
Run Code Online (Sandbox Code Playgroud)
我已经包含了所有必需的头文件和using声明,并file_exists()返回一个布尔值,指示文件是否存在.
我的问题是:为什么上面的代码不正确,正确实现相同目标的最佳方法是什么?
在我的学校,我们必须遵循一种样式指南,该指南指出必须在使用变量的函数顶部声明每个变量,而不是在使用变量之前声明。这通常意味着您必须在循环内使用变量时重置或清除变量,因为它们未在该循环内声明。我不明白为什么每次循环迭代都需要“清除” stringstream变量,并希望有人对此有所了解。我知道如何清除它,只是想知道为什么有必要。
我基本上是在尝试将整数添加到字符串,然后在特定时间将结束符添加到字符串,然后继续添加整数。我希望将它们全部添加到一个字符串对象中。
所以我在字符串流中添加了一个整数,将字符串流馈入了一个字符串。然后在字符串中添加“ \ n”。
然后,我向stringstream添加了另一个int,然后再次将其提供给字符串。
不幸的是,stringstream对象中的第二个对象没有被添加到字符串中,我不确定为什么不能。
我尝试将两个对象添加到我的字符串流对象中,然后一次将它们添加到字符串中(中间有换行符),但是stringstream一次将字符串中的所有内容都添加到了字符串中,所以这是行不通的。
int main(){
string h;
stringstream ss;
ss << 1;
ss >> h;
h += '\n';
ss << 2;
ss >> h;
h += '\n';
cout << h << "endline";
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然没有错误消息。我希望它输出:
1
2
endline
Run Code Online (Sandbox Code Playgroud)
相反,我得到
1
endline
Run Code Online (Sandbox Code Playgroud)
因此,很明显,该字符串会添加我的两个结尾字符,而不是我添加到stringstream的两个字符。