⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rparse.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C17:Rparse.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Reverse the order of words in a string

#include <string>

#include <iostream>

#include <vector>

using namespace std;



int main() {

  // The ';' characters will be delimiters

  string s("now.;sense;make;to;going;is;This");

  cout << s << endl;

  // To store the words:

  vector<string> strings;

  // The last element of the string:

  int last = s.size();

  // The beginning of the current word:

  int current = s.rfind(';');

  // Walk backward through the string:

  while(current != string::npos){

    // Push each word into the vector.

    // Current is incremented before copying to 

    // avoid copying the delimiter.

    strings.push_back(

      s.substr(++current,last - current));

    // Back over the delimiter we just found, 

    // and set last to the end of the next word

    current -= 2;

    last = current;

    // Find the next delimiter

    current = s.rfind(';', current);

  }

  // Pick up the first word - it's not 

  // preceded by a delimiter

  strings.push_back(s.substr(0, last - current));

  // Print them in the new order:

  for(int j = 0; j < strings.size(); j++)

    cout << strings[j] << " ";

} ///:~

⌨️ 快捷键说明

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