C++之容器实例

这几天在实习,经理要求用C++MFC做出五子棋的单机版、局域网版、还有网络大厅版本,趁这个机会赶紧复习了C++关于C++的STL小例子如下:




标准模板库STL应用举例

向量 vector

线性表 list  

队列 queue

映射 map

字符串string

 
1:向量vector类可用来支持动态数组,动态数组是指可以根据需要改变大小的数组。
 
可以很容易地声明一个vector类对象,例如:
vector <int> iv;
vector <int> cv(5);
vector <int> cv(5,’x’);
vector <int> iv2(iv);
// Access a vector using an iterator.
#include <iostream>
#include <vector>
using namespace std;
int main( )
{
  vector<char> v; // create zero-length vector
  int i;
 
  // put values into a vector
  for(i=0; i<10; i++) v.push_back('A' + i);  
// can access vector contents using subscripting
  for(i=0; i<10; i++) cout << v[i] << " ";
  cout << endl;
 
  // access via iterator
  vector<char>::iterator p = v.begin( );
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  return 0;
}
2:
线性表 list 类定义了双向的线性表, 又可称为双向链表。List类只支持顺序访问。
 
下面的C++程序通过实例化链表list类模板建立了一个保存字符的链表,接着使用类模板的排序方法sort( )进行排序,然后输出经过排序后的字符。
// Sort a list.
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
 
int main()
{ int i;
  list<char> lst;  
  // create a list of random characters
  for(i=0; i<10; i++)
    lst.push_back('A'+ (rand()%26));  
cout << "Original contents: ";
  list<char>::iterator p = lst.begin();
  while(p != lst.end()) {
    cout << *p;
    p++;
  }
  cout << endl << endl;
 
  // sort the list
  lst.sort( );  
cout << "Sorted contents: ";
  p = lst.begin();
  while(p != lst.end()) {
    cout << *p;
    p++;
  }  
  return 0;
}
3:
映射map类定义了一个关联容器,并且在容器中使用唯一的关键字来映射相应的值。
map类对象是一系列关键字/值的匹配对。
map的功能在于:只要知道了一个值的关键字,就可以找到这个值。
下面的实例程序通过实例化标准库中的map类模板映射建立了一些英文单词与其反义词的对应关系,利用这种对应系可以迅速查找到一个词的反义词。
// A map of word opposites, using strings.
#include <iostream>
#include <map>
#include <string>
using namespace std;
 
int main( )
{ int i;
  map<string, string> m;
 
  m.insert(pair<string, string>("yes", "no"));
  m.insert(pair<string, string>("up", "down"));
  m.insert(pair<string, string>("left", "right"));
  m.insert(pair<string, string>("good", "bad"));  
string s;
  cout << "Enter word: ";
  cin >> s;
 
  map<string, string>::iterator p;
  
  p = m.find(s);
  if(p != m.end()) 
    cout << "Opposite: " << p->second;
  else
    cout << "Word not in map.\n";
 
  return 0;
}
4:
队列(queue)是一个先进先出(FIFO: First In First Out)的数据结构,在程序设计中经常使用。
 
对一个队列常用的操作有,在队列尾增加一个元素、在队列头取一个元素以及测试队列是否为空、是否为满等操作。
Using queue class in the Standard C++ Library, Instantiate a queue for strings and demonstrate the following functions in main( ) to show that you know how to use this class:
queue::push( )
queue::pop( )
queue::empty( )
queue::front( ) 
queue::back( )
queue::size( )
#include <iostream>
#include <queue>
#include <string>
using namespace std;
void main( )
{queue<string> str_queue;
str_queue.push("string1");
str_queue.push("string2");
str_queue.push("string3");
cout<<"the size of the queue is: "<<str_queue.size()<<endl;
cout<<"the front one "<<str_queue.front()<<endl;
cout<<"the back one "<<str_queue.back()<<endl;
str_queue.pop( );
str_queue.pop( );
str_queue.pop( );
if (str_queue.empty( ))
cout<<" the queue is empty!"<<endl;
}
5:
C++提供了两种处理字符串的方法:
以空字符‘\0’结尾的字符数组
容器类string类的对象(标准库中的string类)
 
使用标准库中的string类的三个理由:
一致性(字符串定义为一种数据类型)
方便性(可以使用标准的运算符)
安全性(不会出现数组越界错误)
//  Demonstrate insert(), erase(), and replace().
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
  string str1("This is a test");
  string str2("ABCDEFG");
 
  cout << "Initial strings:\n";
  cout << "str1: " << str1 << endl;
  cout << "str2: " << str2 << "\n\n";  
// demonstrate insert()
  cout << "Insert str2 into str1:\n";
  str1.insert(5, str2);
  cout << str1 << "\n\n";
 
  // demonstrate erase()
  cout << "Remove 7 characters from str1:\n";
  str1.erase(5, 7);
  cout << str1 <<"\n\n";
  
// demonstrate replace 
  cout << "Replace 2 characters in str1 with str2:\n";
  str1.replace(5, 2, str2);
  cout << str1 << endl;
  
  return 0;
}
结果:
Initial strings:
str1: This is a test
str2: ABCDEFG
 
Insert str2 into str1:
This ABCDEFGis a test
 
Remove 7 characters from str1:
This is a test
 
Replace 2 characters in str1 with str2:
This ABCDEFG a test
 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值