执行以下代码时,我遇到一个错误
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main (int argc, char* argv[]){
string tokens,input;
input = "how are you";
istringstream iss (input , istringstream::in);
while(iss){
iss >> tokens;
cout << tokens << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印出最后一个标记"你"两次,但是如果我做了以下更改,一切正常.
while(iss >> tokens){
cout << tokens << endl;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释我是如何运行while循环的.谢谢
我有一些像这样的输入:(50.1003781N, 14.3925125E)存储在const string & input.我要做的是提取括号,得到代表GPS协调的数字 - 50.1003781,保存N并对第二个坐标执行相同操作.
所以这里是我的代码:
istringstream iStream(input);
char brackets[2]; //To store the brackets
char types[2]; //To store if it is N/S or W/E
char delim; //To store ","
double degrees[2];
iStream >> brackets[0] >> degrees[0] >> types[0] >> delim;
//The delim loads the "," and here goes my problem with whitespace which is next
iStream >> degrees[1] >> types[1] >> brackets[1];
Run Code Online (Sandbox Code Playgroud)
但它加载失败degrees[1],它加载零并tellg()说-1可能是因为逗号后面有空格.解析后的字符串应如下所示:
cout << brackets[0]; // "("
cout << …Run Code Online (Sandbox Code Playgroud) 我有这个对象:
istringstream ob_stream;
Run Code Online (Sandbox Code Playgroud)
我需要将该对象转换为字符串和char数组才能工作。怎么做?
我有一些代码使用 fork、execlp 和 wait 来创建两个进程。目标是能够重复打印提示并让用户输入命令,该命令最多包含 4 个参数/选项。
int main()
{
string command, argument;
istringstream iss(argument);
do
{
//prompt user for command to run
cout << "Enter command: ";
cin >> command >> argument;
int pid, rs, status;
//fork will make 2 processes
pid = fork();
if(pid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if(pid == 0) {
//Child process: exec to command with argument
//C_str to get the character string of a string
rs = execlp(command.c_str(), command.c_str(), argument.c_str(), (char*) NULL);
. …Run Code Online (Sandbox Code Playgroud) 我对c ++很新,遇到了一个问题,单独搜索我找不到解决方案.
问题是,为什么istringstream永远不会在下面的循环中改变它的值?
它从dirs [0]和dirs [1]获取值,并且永远不会将它们更改为增加的int i.顺便说一句.dirs [i]和dirs [i + 1]中的值存储为十六进制值(例如0F9C8924).
下面是我的最新设置,我已经尝试了其他几种方法,但没有成功,例如在循环内部使用istringstream和ios_base :: trunc以及任何方式.
还dirs[ i ]等确实有不同的价值观和正确地读,而是试图通过时向istringstream使进制字符串成一个无符号整型它永远不会需要新的价值观.
unsigned int f;
unsigned int t;
istringstream ss;
istringstream ss2;
for( int i = 0; i < count; i+=3 ) {
ss.clear();
ss2.clear();
ss.str( dirs[ i ] );
ss2.str( dirs[ i + 1 ] );
ss >> f;
ss2 >> t;
// do something else with dirs[ i + 3 ], not relevant
}
Run Code Online (Sandbox Code Playgroud)
count并且dirs是一个全局变量,并count在另一个函数中增加,它是值的计数dirs …
我有以下代码使用strtok接收来自txt文件的输入.txt文件中的输入是:
age, (4, years, 5, months)
age, (8, years, 7, months)
age, (4, years, 5, months)
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像:
char * point;
ifstream file;
file.open(file.c_str());
if(file.is_open())
{
while(file.good())
{
getline(file, take);
point = strtok(&take[0], ", ()");
}
}
Run Code Online (Sandbox Code Playgroud)
除了第2年龄和第3年龄的输出缺失外,它一切正常.谁能告诉我他们为什么失踪?
我也试过,istringstream但每当我输入我的文件名时,程序崩溃了.
char * point;
char take[256];
ifstream file;
file.open(file.c_str());
if(file.is_open())
{
while(file.good())
{
cin.getline(take, 256);
point =strtok(take,", ()");
}
}
Run Code Online (Sandbox Code Playgroud) 乡亲!我一直在努力解决这个问题,到目前为止我还没有找到任何解决方案.
在下面的代码中,我使用数字初始化一个字符串.然后我使用std :: istringstream将测试字符串内容加载到double中.然后我提出两个变量.
#include <string>
#include <sstream>
#include <iostream>
std::istringstream instr;
void main()
{
using std::cout;
using std::endl;
using std::string;
string test = "888.4834966";
instr.str(test);
double number;
instr >> number;
cout << "String test:\t" << test << endl;
cout << "Double number:\t" << number << endl << endl;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
当我运行.exe时,它看起来像这样:
字符串测试:888.4834966
双号888.483
按任意键继续...
字符串有更多的数字,看起来像std :: istringstream只加载了6中的10个.如何将所有字符串加载到double变量中?
我有一串数字.我试图使用istringstream将其作为int类型打印在字符串中的每个数字.如果将整个字符串作为参数传递给main中的转换函数,它可以正常工作但如果我通过索引传递它,则会引发错误.
如何使用索引使此代码工作以将字符串数组中的每个数字作为int打印.
这是我的代码.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int strToNum(string str)
{
istringstream ss(str);
int n;
ss>>n;
cout<<n;
}
int main()
{
string str = "123";
for(int i=0; i<str.length(); i++)
//strToNum(str); Works fine
strToNum(str[i]); //raises error
}
Run Code Online (Sandbox Code Playgroud) 我注意到当我使用istringstream时eof()即使整个字符串被"消耗"也不会返回true.例如:
char ch;
istringstream ss{ "0" };
ss >> ch;
cout << ss.peek() << " " << (ss.eof() ? "true" : "false");
Run Code Online (Sandbox Code Playgroud)
输出(VS2015):
-1 false
Run Code Online (Sandbox Code Playgroud)