C++必备STL的高频应用实例

版权声明:本文为博主原创文章,转载请务必注明出处和作者,谢谢合作! https://blog.csdn.net/zhanshen112/article/details/84112375

string

1、常用操作

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

int main(){
	string s1;	//初始化字符串,空字符串 
	string s2=s1;	//s2为s1的副本 
	string s3="i am yasuo";	//直接初始化并赋值 
	string s4(10,'a');	//s4存的字符串是aaaaaaaaaa
	string s5(s4);	//s5为s4的副本
	string s6("i am ali");
	string s7=string(6,'c'); 
	
	cout<<"s6.size="<<s7.size() <<endl;
	cout<<(s2.empty() ?"this is a empty string":"this is not a empty string")<<endl; 
} 
/*
output:
s6.size=6
this is a empty string

--------------------------------
Process exited after 0.785 seconds with return value 0
请按任意键继续. . .
*/

2、迭代器iterator在string的应用

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

int main(){
	string str("hello world");
	//使用iterator遍历字符串 
	for(string::const_iterator it=str.begin() ;it!=str.end() ;it++){
		cout<<*it<<endl;
	}
} 
/*
output:
h
e
l
l
o

w
o
r
l
d

--------------------------------
Process exited after 0.6435 seconds with return value 0
请按任意键继续. . .

*/

3、string的查找子串等功能

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

int main(){
	string str("hello world");
	cout<<str.find("ll",0)<<endl;
	if(str.find("aa",0)==string::npos )
		cout<<"找不到该子串"<<endl; 
} 
/*
2
找不到该子串

--------------------------------
Process exited after 0.6054 seconds with return value 0
请按任意键继续. . .

*/

vector

1、vector常用操作

//11152310
#include <iostream>
#include <vector>
using namespace std;

int main(){
	vector<int> v1;
	//存入vector 
	for(int i=0;i<10;i++)	v1.push_back(i);
	//输出vector,但是注意要知道vector的长度,此方法不太好 
	for(int i=0;i<10;i++)	cout<<v1[i]; 
	
	cout<<endl;
	
	//顺序输出通法。使用iterator迭代器输出 ,默认顺序输出 
	for(vector<int>::iterator it=v1.begin() ;it!=v1.end() ;it++ )
		cout<<*it;
	
	cout<<endl;
	
	//反向输出通法。使用reverse_iterator迭代器逆序输出 
	for(vector<int>::reverse_iterator it=v1.rbegin() ;it!=v1.rend() ;it++ )
		cout<<*it;
	
}
/*
0123456789
0123456789
9876543210
--------------------------------
Process exited after 0.4554 seconds with return value 0
请按任意键继续. . .
*/

2、

#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
template <typename T>
void showvector(vector<T> v)
{
    for (vector<T>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it;
    }
    cout << endl;
}
 
int main()
{
    vector<string> v6 = { "hi","my","name","is","lee" };
    v6.resize(3);  //重新调整vector容量大小
    showvector(v6);
 
    vector<int> v5 = { 1,2,3,4,5 }; //列表初始化,注意使用的是花括号
    cout << v5.front() << endl; //访问第一个元素
    cout << v5.back() << endl; //访问最后一个元素
 
    showvector(v5);
    v5.pop_back(); //删除最后一个元素
    showvector(v5);
    v5.push_back(6); //加入一个元素并把它放在最后
    showvector(v5);
    v5.insert(v5.begin()+1,9); //在第二个位置插入新元素
    showvector(v5);
    v5.erase(v5.begin() + 3);  //删除第四个元素
    showvector(v5);
    v5.insert(v5.begin() + 1, 7,8); //连续插入7个8
    showvector(v5);
    v5.clear(); //清除所有内容
    showvector(v5);
 
    system("pause");
    return 0;
} 

map

set

list

猜你喜欢

转载自blog.csdn.net/zhanshen112/article/details/84112375