stringseeking.cpp

来自「C语言数值算法程序大全(第二版),课本文件」· C++ 代码 · 共 37 行

CPP
37
字号
//: C04:StringSeeking.cpp
// From "Thinking in C++, 2nd Edition, Volume 2"
// by Bruce Eckel & Chuck Allison, (c) 2003 MindView, Inc.
// Available at www.BruceEckel.com.
// Reads and writes a string stream
//{-bor}
#include <cassert>
#include <sstream>
#include <string>
using namespace std;

int main() {
  string text = "We will sell no wine";
  stringstream ss(text);
  ss.seekp(0, ios::end);
  ss << " before its time.";
  assert(ss.str() ==
    "We will sell no wine before its time.");
  // Change "sell" to "ship"
  ss.seekg(9, ios::beg);
  string word;
  ss >> word;
  assert(word == "ell");
  ss.seekp(9, ios::beg);
  ss << "hip";
  // Change "wine" to "code"
  ss.seekg(16, ios::beg);
  ss >> word;
  assert(word == "wine");
  ss.seekp(16, ios::beg);
  ss << "code";
  assert(ss.str() ==
    "We will ship no code before its time.");
  ss.str("A horse of a different color.");
  assert(ss.str() == "A horse of a different color.");
} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?