[C/C++标准库]_[初级]_[使用string stream]


sstream

stringstream

ostringstream

istringstream


场景:

1.需要对字符进行逐个处理,比如加密.

2.int,float和string互相转换.

3.简单提取以isspace分割的字符串或数字.如 "ab asdf 3 4 5  6 7 8 9 10"

4.以单个字符为分割符来分割字符串.

#include <stdlib.h>
#include <sstream>

#include <iostream>

using namespace std;

void TestConvertStringToFloat()
{
  istringstream stream1;
  float num;    

  // use it once
  //1.支持isspace分割
  string string1 = "23    1 3.235\n1111111\n222222";
  stream1.str(string1);
  while( stream1 >> num ) cout << "num1: " << num << endl;  // displays numbers, one per line

  // use the same string stream again with clear() and str()
  string string2 = "1 2 3 4 5  6 7 8 9 10";
  stream1.clear();
  stream1.str(string2);

  while( stream1 >> num ) cout << "num2: " << num << endl;  // displays numbers, one per line

  string string3 = "ab asdf 3 4 5  6 7 8 9 10";
  string mystr;
  stream1.clear();
  stream1.str(string3);

  while( stream1 >> mystr ) cout << "mystr: " << mystr << endl;  // displays numbers, one per line

}

void TestConvertFloatToString()
{
    stringstream stream1;
    stream1 << 123;
    string str = stream1.str();
    cout << str << endl;
    
    stream1.seekp(0,std::ios::beg); //1.重复使用时注意移动到stringstream前面.
    stream1 << 3.2;
    str = stream1.str();
    cout << str << endl;
}

void TestGetLineWithStringStream()
{
  //1.线程安全的,但是只能以字符作为分隔符
  stringstream ss("google|twitter|facebook|microsoft|apple|ibm|");
  string str;
  while(getline(ss,str,'|'))
  {
    cout << str << endl;
  }
}

//1.Characters can be inserted and/or extracted from 
//the stream using any operation allowed on both input and output streams.
//2.即它可以像流那样对字符进行逐个处理,可用于转码.
int main(int argc, char const *argv[])
{
    cout << "TestConvertIntToString" << endl;
    TestConvertFloatToString();
  cout << "TestConvertStringToFloat" << endl;
    TestConvertStringToFloat();
  cout << "TestGetLineWithStringStream" << endl;
  TestGetLineWithStringStream();

    return 0;
}

//参考
//1. http://www.cplusplus.com/reference/sstream/stringstream/
//2. http://www.usidcbbs.com/read-htm-tid-1898.html
//3. http://faculty.virginia.edu/comp-phys/phys2660/html/references/www.cppreference.com/cppsstream/all.html
//4. http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c

输出:

TestConvertIntToString
123
3.2
TestConvertStringToFloat
num1: 23
num1: 1
num1: 3.235
num1: 1.11111e+006
num1: 222222
num2: 1
num2: 2
num2: 3
num2: 4
num2: 5
num2: 6
num2: 7
num2: 8
num2: 9
num2: 10
mystr: ab
mystr: asdf
mystr: 3
mystr: 4
mystr: 5
mystr: 6
mystr: 7
mystr: 8
mystr: 9
mystr: 10
TestGetLineWithStringStream
google
twitter
facebook
microsoft
apple
ibm
[Finished in 0.1s]

2015.5.19 补充.

1. stringstream作为sprintf的替换和重用stringstream.

#include "stdafx.h"

#include <sstream>
#include <string.h>
#include <string>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	//1. 使用stringstream来存储不定长字符串.
	std::stringstream ss;
	int i = 10;
	int j = 20;
	ss << i << " is smaller than " << j;
	std::cout << ss.str() << std::endl;
	
	//1. C使用sprintf来实现.
	char buf[32];
	sprintf(buf,"%d is smaller than %d",i,j);
	std::cout << buf << std::endl;

	//1. 重用ss.必须调用clear()和str("")
	//////////
	ss.clear();
	ss.str("");
	ss << "hello world";
	std::cout << ss.str() << std::endl;

	return 0;
}

输出:

10 is smaller than 20
10 is smaller than 20
hello world



你可能感兴趣的:(C++,Stream,数字,sstream,stringstream)