欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(8)chapter8 类:IO库-string流

发布时间:2025/3/21 c/c++ 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++ Primer 5th笔记(8)chapter8 类:IO库-string流 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

sstream输入输出类继承自iostream类
. istringstream读取一个字符串
. ostringstream写入一个字符串

1. 可以使用iostream类的操作

sstream strm; //一个未绑定的stringstream sstream strm(s); //strm持有一个string s,这个构造函数是explicit strm.str(); //返回一个string的copy strm.str(s); //将string复制进strm,返回void

stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗适时地清除一下缓冲 (用 stream.str("") )。

2. 字符串变基本数据类型

/字符串 变 double/
void static ioString1()
{
double n;
string str = “12.5”;
stringstream stream;
stream << str;
stream >> n;
cout << " " << n;
}
3. istringstream

void static ioString2(){istringstream istr;string line, str;while (getline(cin, line))//从终端接收一行字符串,并放入字符串line中 {istr.str(line);//把line中的字符串存入字符串流中 while (istr >> str)//每次读取一个单词(以空格为界),存入str中 {cout << str << endl;}} }

4. ostringstream

void static ioString3(){//初始化输出字符串流ostr ostringstream ostr("1234");cout << ostr.str() << endl;//输出1234 ostr.put('5');//字符4顶替了1的位置 cout << ostr.str() << endl;//输出5234 ostr << "67";//字符串67替代了23的位置,输出5674 string str = ostr.str();cout << str << endl;}

4. istringstream + ostringstream :

struct PersonInfo {string name;vector<string> phones;};// we'll see how to reformat phone numbers in chapter 17// for now just return the string we're givenstatic string format(const string& s) { return s; }vector<PersonInfo> static getData(istream& is){// will hold a line and word from input, respectivelystring line, word;// will hold all the records from the inputvector<PersonInfo> people;// read the input a line at a time until end-of-file (or other error)while (getline(is, line)) {if (line.length() < 2)break;PersonInfo info; // object to hold this record's dataistringstream record(line); // bind record to the line we just readrecord >> info.name; // read the namewhile (record >> word) // read the phone numbers info.phones.push_back(word); // and store thempeople.push_back(info); // append this record to people}return people;}static ostream& processOS(ostream& os, vector<PersonInfo> people){for (const auto& entry : people) { // for each entry in peopleostringstream formatted, badNums; // objects created on each loopfor (const auto& nums : entry.phones) { // for each number // ``writes'' to formatted's stringformatted << " " << format(nums);}if (badNums.str().empty()) // there were no bad numbersos << entry.name << " " // print the name << formatted.str() << endl; // and reformatted numbers else // otherwise, print the name and bad numberscerr << "input error: " << entry.name<< " invalid number(s) " << badNums.str() << endl;} return os;}static void ioString4(){ vector<PersonInfo> vec = getData(cin);processOS(cout, vec); }

【引用】

  • 代码 https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/ioTest.h
  • 总结

    以上是生活随笔为你收集整理的C++ Primer 5th笔记(8)chapter8 类:IO库-string流的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。