码迷,mamicode.com
首页 > 编程语言 > 详细

C++中STL的一些代码

时间:2015-05-09 11:52:16      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:c++   stl   

 

          这次把C++中的STL的一些东西练习一下下,STL全称为  Standard Template Library ,也就是标准模板库,  要使用STL,要了解以下几个基本概念:

        容器:可以把它理解为存放数据的地方,常用的一些容器有 链表(list) 栈(stack) 动态数组 (vector) 双端队列(deque) 队列(queue) 映射(map)

        游标(iterator):可以把它理解为指针类型,STL中的许多函数需要用到它们作为参数

        算法:它们通常需要与容器和游标配合使用,使用它们,你可以方便地对容器中的数据进行各种常见的操作,如排序操作,寻找最大元素的操作等

 

       

        我只是简单的练习容器, 这个先是list双向链表代码,也只是简单的练习下,害怕自己忘记而已。

#include <iostream>
#include <list>
#include <cstdlib>

using namespace std;
		
int main(int argc, char** argv) {
    int i;
    list<char>lst;
    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;
    //sort the list
    lst.sort();
    cout<<"Sorted contents: ";
    p = lst.begin();
    while( p != lst.end()){
    	cout<<*p;
    	p++;
    }
		return 0;
}


      


map的使用

#include <iostream>
#include <map>
#include <string>

using namespace std;
		
int main(int argc, char** argv) {
      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>("cool","tong"));
	  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;
}

 

queue的应用

 

#include <iostream>
#include <queue>
#include <string>

using namespace std;
		
int main(int argc, char** argv) {
     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;
		return 0;
}


vector的使用

#include <iostream>
#include <vector>

using namespace std;
		
int main(int argc, char** argv) {
    vector<char>v;
    int i;
    
    for(i=0;i<10;i++)
       v.push_back('A'+i);
    for(i=0;i<10;i++)
       cout<<v[i]<<" ";
    cout<<endl;
    
    vector<char>::iterator p = v.begin();
	while( p!= v.end()){
		cout<<*p<<" ";
		p++;
	}
		return 0;
}


string的使用

#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
	string a="abcedjnmbvnbm, cnm,bncm,bnm, nm,cnmm,klndslkfl sdnsdklnf ls";
	cout<<a.length()<<endl;
	cout<<a.size()<<endl;
	cout<<a.capacity()<<endl;
	cout<<a.max_size()<<endl;	
	return 0;
}

*************************************************************
#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
	string a="abcdefghi";
	string b="123456789";
	string c="啦啦啦啦"; 
	
	cout<<"比较字符串a和b"<<endl;
	cout<<a.compare(b)<<endl;
    
	a.swap(b);
    cout<<"交换后a和b的输出"<<endl<<"输出a="<<a<<endl<<"输出b="<<b<<endl;
    
    cout<<"查找"<<endl;
    cout<<b.find('e',0)<<endl;
    
    cout<<"熟悉replace"<<endl;
	cout<<b.replace(0,5,a)<<endl;
	
	cout<<"插入insert"<<endl;
    cout<<b.insert(5,c)<<endl;
    
	return 0;
}

*********************************************************************
#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
   string str("this is an example sencetence.");
   cout<<str<<endl;
   
   str.erase(10,8);
   cout<<str<<endl;
   
   str.erase(str.begin()+9);  //删除第9个 
   cout<<str<<endl;
   
    str.erase(str.begin()+5,str.end()-9);
    cout<<str<<endl;
	return 0;
}
定义两个string类型的字符串,然后调用erase(),这个主要还是参数问题。只有一个参数就表示删除第几个位置的字符,begin()和end()只不过是开始和结束的位置,

******************************************************************************************
#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
     string str("there are two needles in this haystack with needles.");
     string str2("needle");
     int found=str.find(str2);
     if(found!=-1)
        cout<<"first 'neeedle'found at:"<<found<<endl;
     
	 found=str.find("needle are small",found+1,6);//从pos开始查找字符串s中前n个字符在当前串中的位置
     if(found!=-1)
        cout<<"second 'needle' found at: "<<found<<endl ;
        
     found=str.find("haystack");
     cout << "'haystack' also found at: " << found <<endl;
     
     found=str.find('.');
     if(found!=-1)
         cout << "Period found at: " << found << endl;
   // 从str在字符串中找到str2的首位置并删除,然后加上后面的字符串 
     str.replace(str.find(str2),str2.length(),"preposition");
     cout<<str<<endl;
	return 0;
}
*****************************************************************************************************
#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
     string base="this is a test string .";
	 
	string str2="n example";
	
	string str3="sample phrase";
	
	string str4="useful.";
	
	string str=base;
	
	cout<<str.replace(9,5,str2)<<endl;
	cout<<str.replace(19,6,str3,7,6)<<endl;
	cout<<str.replace(8,10,"just a")<<endl;
	cout<<str.replace(8,6,"a shorty",7)<<endl;
	cout<<str.replace(22,1,3,'!')<<endl;
	cout<<str<<endl;
	
	return 0;
}




运行结果
this is an example string .
this is an example phrase .
this is just a phrase .
this is a short phrase .
this is a short phrase!!!.
this is a short phrase!!!.

--------------------------------
Process exited with return value 0
Press any key to continue . . .


*********************************************************************************************************************************


#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
	
    char ChangAn[20]={'\0'};
    string str("Xi'an university of post & Telecommunication");
	int length =str.copy(ChangAn,10,6);
	
	cout<<"ChangAn contains: "<<ChangAn<<endl;
	return 0;
}


copy是从字符串第6个位置开始然后复制10个字符到数组

*********************************************************************************************************************************
#include <iostream>
#include<string>
using namespace std;

int main(int argc, char** argv) {
	
     string firstlevel("cn");
     string secondlevel("edu");
     string thirdlevel("xupt");
     string scheme("http://");
     string hostname;
     string url;
     
     hostname="www."+thirdlevel+'.'+secondlevel+'.'+firstlevel;
     url=scheme+hostname;
     
     cout<<url<<endl;
     
	return 0;
}
在C++中+可以进行字符串的连接
*********************************************************************************************************************************
#include <iostream>

#include <string>

int main ()
{
  string str1 ("green apple");

  string str2 ("red apple");
  if (str1.compare(str2) != 0)
   cout << str1 << " is not " << str2 << '\n';
  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";
  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";
  if (str1.compare(6,5,str2,4,5) == 0)
  cout << "therefore, both are apples\n";
  return 0;







 

 

 

 

C++中STL的一些代码

标签:c++   stl   

原文地址:http://blog.csdn.net/lotluck/article/details/45599091

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!