对字符串C ++中的所有整数求和 [英] Sum all integers in a string C++
问题描述
我的代码中有一个C ++字符串,如下所示:
1 2 3 4 5 6 7 8
我知道字符串是由空格字符分隔的整数组成的。
我是一个C ++新手,在Java中我只需要做:
String str =1 2 3 4 5 6 7 8;
int sum = 0;
for(int i = 0; i sum + = Integer.parse(str.split )[i];
}
对象在C ++?
有人建议我 stringstream
但我还是不能理解这个对象,
更新:有些人很好地试图帮助我,但仍然没有工作,也许是因为我的问题的一些怪癖,我以前没有澄清,所以这里:
#include< iostream>
#include< string>
#include< sstream>
using namespace std ;
int main()
{
freopen(variable-exercise.in,r,stdin);
$ b b int sum = 0,start = 0;
string line;
while(getline(cin,line)){
istringstream iss(line);
while(iss>> start){
sum + = start;
}
cout<<开始<< endl;
sum = start = 0;
}
return 0;
}
Ah,输入文件包含以下内容:
1
3 4
8 1 1
7 2 9 3
1 1 1 1 1
0 1 2 5 6 10
因此,对于每一行,程序必须打印在字符串行中的所有整数。此示例将生成:
1
7
10
21
5
24
感谢
有人建议我使用stringstream,但是我还是不能理解这个对象,我需要完全读取这个字符
我想你给了一个很好的建议。使用 std :: istringstream
,您可以只读在一个接一个的值,因为你会从标准输入(或任何其他输入流)中读取它们。
例如:
#include< sstream&
#include< string>
#include< iostream>
int main()
{
//假设在某些时候你有这个字符串...
std :: string s =1 2 3 4 5 6 7 8 9 10;
//你可以从中创建一个istringstream对象...
std :: istringstream iss(s);
int i = 0;
int sum = 0;
//一个接一个读取所有值...
while(iss>> i)
{
// ...每次
sum + = i时更新总和;
}
std :: cout<<和;
}
I have a C++ string in my code that is like:
"1 2 3 4 5 6 7 8"
I know the string is composed of integers separated by a space char. How can I sum them?
I'm quite a C++ newbie and in Java I'd simply do:
String str = "1 2 3 4 5 6 7 8";
int sum = 0;
for (int i = 0; i < str.split(" ").length; i++ {
sum += Integer.parse(str.split(" ")[i];
}
How can I do just like this with my string object in C++?
Some people suggested me stringstream
but I still can't understand this object and I need to read the string entirely, getting every single digit within it.
Thanks in advance!
Update: some guys nicely tried to help me but still it's not working. Perhaps because of some quirk of my problem which I haven't clarified before. So here it goes:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
freopen("variable-exercise.in", "r", stdin);
int sum = 0, start = 0;
string line;
while(getline(cin ,line)) {
istringstream iss(line);
while(iss >> start) {
sum += start;
}
cout << start << endl;
sum = start = 0;
}
return 0;
}
Ah, the input file contains the following:
1
3 4
8 1 1
7 2 9 3
1 1 1 1 1
0 1 2 5 6 10
So, for each line, the program must print the sum of all integers in the string line. This example would generate:
1
7
10
21
5
24
thanks
Some people suggested me stringstream but I still can't understand this object and I need to read the string entirely
I guess you were given a good advice. With std::istringstream
you could just read in values one after the other as you would read them from the standard input (or any other input stream).
For instance:
#include <sstream>
#include <string>
#include <iostream>
int main()
{
// Suppose at some time you have this string...
std::string s = "1 2 3 4 5 6 7 8 9 10";
// You can create an istringstream object from it...
std::istringstream iss(s);
int i = 0;
int sum = 0;
// And read all values one after the other...
while (iss >> i)
{
// ...of course updating the sum each time
sum += i;
}
std::cout << sum;
}
这篇关于对字符串C ++中的所有整数求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!