C ++ single-line string summation (Code tutorial), summation

Source: Internet
Author: User

C ++ single-line string summation (Code tutorial), summation

C ++ single-line string summation (Code tutorial)

# Include <iostream> # include <string> # include <sstream> using namespace std; int main () {string line; while (getline (cin, line )) {// write the entire row of data (together with spaces) into line. If you enter string in the cin> mode, int sum = 0, x; stringstream ss (line) is stopped ); // create a sequence stream string ss while (ss> x) {// read ss to x sum + = x ;}cout <sum <endl ;} return 0 ;}

Getline () is defined in the <string> header file. The function is to take a line string, read the line break \ n, and discard the line break. If you need to read the character, read the next line.

Getline (cin, str); the first parameter is the input stream object, and the second parameter is the string object.

Stringstream, derived from iostream, provides the string read/write function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.