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

📄 stringlist.cpp

📁 适合初学者学习以及程序员回顾
💻 CPP
字号:
// StringList.cpp

#include <iomanip>
#include <list>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
using std::list;
using std::string;
using std::ostream_iterator;
using std::cout;
using std::endl;

int main()
{
  list<string>  S;
  list<string>::iterator Index;
  // 定义 ostream_iterator 物件
  ostream_iterator<string>  StringOut(cout, "  ");
  // 加入元素
  S.push_front("John");
  S.push_front("Memi");
  S.push_front("Brigitte");
  S.push_back("Daiana");
  S.push_back("Lulu");
  S.push_back("Lisa");
  
  cout << "\nlist S 原来为: "  << endl;
  copy(S.begin(), S.end(), StringOut);
  cout << endl;
  // 另外一种表示法
  cout << "\n另外一种表示法" << endl; 
  for (Index = S.begin(); Index != S.end(); Index++)
        cout << *Index << "  ";
  cout << endl;
  // 插入
  S.insert(find(S.begin(), S.end(), "Daiana"), "Joanne");
  cout << "\n在 Daiana 前插入 Joanne" 
       << " 之后, list S 为: "  << endl;
  copy(S.begin(), S.end(), StringOut);
  cout << endl;
  // 移除
  S.remove( "Brigitte");
  cout << "\n在移除 Brigitte" 
       << " 之后, list S 为: "  << endl;
  copy(S.begin(), S.end(), StringOut);
  cout << endl;
  // 取代
  *find(S.begin(), S.end(), "Daiana")= "William";
  cout << "\n将 Daiana 取代为 William" 
       << " 之后, list S 为: "  << endl;
  copy(S.begin(), S.end(), StringOut);
  cout << endl;
  // 将 list 物件各元素的值依大小排序
  S.sort();
  cout << "\n排序之后, S 变成为:"
       << endl;
  std::copy(S.begin(), S.end(), StringOut);

  return 0;
}

⌨️ 快捷键说明

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