这周STL学习了以下几种:
1.string类
(1)string表示可变长度的字符序列
字符串是对象
(2)string类支持字符串对象的各种操作
各种初始化方式
字符串之间的复制·比较·链接
查询字符串长度和判断字符串是否为空
访问字符串中的单个字符
用string解决过的问题:
例.计算书费:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct book
{
string name; //此处将string当做字符串数组使用
double money;
};
book b[10]={{"计算概论",28.9},
{"数据结构与算法",32.7},
{"数字逻辑",45.6},
{"C++程序设计教程",78},
{"人工智能",35},
{"计算机体系结构",86.2},
{"编译原理",27.8},
{"操作系统",43},
{"计算机网络",56},
{"JAVA程序设计",65}
};
int main()
{
int k,i,j;
double sum=0,n;
cin>>k;
for(j=0;j<k;j++)
{ for(i=0;i<10;i++)
{ cin>>n;
sum+=n*b[i].money;
}
cout<<fixed<<setprecision(2)<<sum<<endl;
sum=0;
}
return 0;
}
string操作
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2; //创建两个空字符串对象
string s3 = "Hello,world!"; //初始化
string s4("I am");
s2 = "Today"; //赋值
s1 = s3 + " " + s4; //字符串连接
s1 += "5" ; //末尾追加
cout << s1 + s2 + "!" << endl; //输出字符串内容
cout <<"Length of s1 is :" << s1.size() << endl; //输出s1的长度
for ( int i = 0 ; i < s1.size() ; i++)
cout << s[i] << " "; //逐个数出s1中的字符
}
string 还有如图所示的其它很多操作:
2.栈(Stack)
Stack是一种先进后出的数据结构,它只有一个出口,只能操作最顶端元素。示意图:
操作
#include<stack> //头文件
stack<int>s; //定义,其中,s为int型,int要用<>括起来
//操作:
empty()--返回bool型,表示栈内容是否为空(s.empty())
size()--返回栈内元素个数(s.size())
top()--返回栈顶元素值(s.top())
pop()--移除栈顶元素(s.pop())
push(data_type a)--向栈压入一个元素a(s.push(a);)
代码操作:
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int main{
stack<int>s;
s.push(1);
s.push(2);
s.push(3); //栈顶
cout << "Top:" << s.top() << endl;
cout << "Size:" << s.size() << endl;
s.pop(); //3被删掉了
cout << "Size:" << s.size() << endl;
if(s.empty()){
cout << "Is empty" << endl; }
else{ cout << "Is not empty" << endl;}
return 0;
}
3.队列(queue)
queue是一种先进先出的数据结构,从低端加入元素,从顶端取出元素
图示:
头文件:#include<queue>
定义:queue <date_type> queue_name;
如:queue <int> q;
操作:
empty()--返回bool类型,表示queue是否为空(q.empty())
size()--返回queue内元素个数(q.size())
front()--发返回queue内的下一个元素(q.front())
back()--返回queue内的最后一个元素(q.back())
pop()--移除queue中的一个元素(q.pop())
push(date_type a)--将一个元素a置入queue中(q.push(a))
代码操作:
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
queue<int>s;
s.push(1);
s.push(2);
s.push(3);
cout << "Front:" << s.front() << endl;
cout << "Back:" << s.back() << endl;
s.pop();
cout << "Size:" << s.size() << endl;
cout << "Front:" << s.front() << endl;
cout << "Back:" << s.back() << endl;
return 0;
}
4.vector
操作:
头文件:#include<vector>
定义: vector<date_type>vector_name;
如: vector<int>v;
操作:
empty()--返回bool型,表示vector是否为空(v.empty())
size()--返回vector内元素个数(v.size())
push_back(date_type a)将元素a插入最尾端
pop_back()将最尾端元素删除
v[i]类似数组取第i个位置的元素(v[0])
代码操作:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
vector<int>a;
for(int i=0;i<5;i++){
a.push_back(5-i);
}
cout<<"Size:"<<a.size()<<endl;
a.pop_back();
a[0]=1;
cout<<"Size:"<<a.size()<<endl;
for(int i=0;i<(int)a.size();i++){
cout<<a[i]<<","<<endl;
}
cout<<endl;
return 0;
}
5.sort
头文件:#include<algorithm>
sort(begin,end);
sort(begin,end,cmp);
例:
int num[]={1,5,6,2,9};
1)sort(num,num+5); //默认从小到大排序num[]={1,2,5,6,9}
2)bool cmo(int a,int b){
return a>b;
}
sort(num,num+5,cmp); //num[]={9,6,5,2,1};
代码操作:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int a[10];
vector <int>b;
ofr(int i=0;i<5;i++)
{
cin>>a[i];
b.push_back(a[i]);
}
sort(a,a+5);
sort(b.begin(),b.endl());
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=0;i<5;i++)
cout<<b[i]<<" ";
return 0;
}
6.优先队列(priority_queue)
一个拥有权值观念的queue,自动依照元素的权值排列,权值最高排在前面。缺省的情况下,priority_queue是利用一个max_heap完成的。示意图:
操作:
头文件: #include
定义:priority_queue <data_type> priority_queue_name;
如:priority_queue q;//默认是大顶堆
操作:
q.push(elem) 将元素elem置入优先队列
q.top() 返回优先队列的下一个元素
q.pop() 移除一个元素
q.size() 返回队列中元素的个数
q.empty() 返回优先队列是否为空
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define pow2(a) ((a)*(a))
#define dist2(x, y) (pow2(x) + pow2(y))
struct coord{
int x, y;
const bool operator<(const coord &b)const{
return (dist2(x, y) < dist2(b.x, b.y));
}
};
学习感想
STL知识点繁多需要耐下心来慢慢学习,虽然列举出来了这些,但掌握还需要进行大量的实践。