一、查找和排序
1.排序
sort()函数
头文件:<algorithm>
sort(begin,end,com),com参数可以没有,默认为升序
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int a[5]={1,3,4,2,5};
sort(a,a+5);
for(int i=0;i<5;i++)
cout<<a[i]<<' ';
return 0;
}
注意:参数的begin和end为地址,如对数组data[6]的排序为
sort(data,data+6)//默认升序排序
sort(data,data+6,greater<int>())//降序排序
二级排序(如结构体)
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct link
{
int a,b;
};
bool cmp(link x,link y)
{
if(x.a==y.a)
return x.b>y.b;
return x.a>y.a;
}
int main()
{
link x[4];
for(int i=0;i<4;i++)
cin>>x[i].a>>x[i].b;
sort(x,x+4,cmp);
for(int i=0;i<4;i++)
cout<<x[i].a<<' '<<x[i].b<<endl;
return 0;
}
需要自己写com函数,bool类型。com()函数工作原理是,如果返回值是0则互换位置,返回值是1位置不变。
或者使用functional里面的函数。functional提供了一堆基于模板的比较函数对象。
equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>
2.查找
线性查找、二分查找
binary_search()函数
头文件:<algorithm>
binary_search(begin,end,target),注意:begin和end为被查数组的地址,要求数组元素非递减
二分查找代码
bool BinarySearch(int n,int target)
{
int left = 0;
int right = n - 1;
while(left <= right)
{
int middle = (left + right) / 2;
if(data[middle] < target)
left = middle + 1;
else if(data[middle] > target)
right = middle - 1;
else
return true;
}
return false;
}
参考:C++ STL中的Binary search(二分查找)
二、字符串
C++数据类型字符串string
库: <string>
初始化:
输入:
string str;
cin >> str;
可以用字符串常量初始化
string str = "hello";
元素访问:可以采用已经重载好的运算符[index]访问或者str.at(index)
操作符:可以用 ==、>、<、>=、<=、和!=比较字符串,可以用+或者+=操作符连接两个字符串,并且可以用[]获取特定的字符
函数:
字符串操作 | |
---|---|
size() | 返回字符串长度,有几个字符(不包括\0) |
length() | 和size()功能基本相同 |
insert(int index,string s) | 从下表index开始,插入字符串s |
erase(int index,int length) | 从index开始,删除length个字符 |
erase(int index) | 删除index后面所有的东西(保留index上的,从1计数) |
clear() | 清空字符串 |
字符串查找 | |
find("string substring") | 找到返回其下标(多个返回第一次出现的位置),找不到返回string::npos |
substr(int index1,int index2) | 返回一个子串string,从下标index1到index2的字符串,从0计数,深拷贝 |
swap(string s) | s1.swap(s2),字符串的内容交换 |
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str = "hello world hello world";
cout << str << endl;
int found = str.find("world");//寻找有的,多个的
printf("world found at %d\n",found);
found = str.find("123");//寻找没有的
printf("123 found at %d",found);
return 0;
}
输出:
hello world hello world
world found at 6
123 found at -1
三、数据结构——向量(vector)
vector是可以改变大小的线性序列容器。和数组一样,向量使用连续的空间存储元素,也可以使用下表访问元素,但是向量的大小可以动态变化。在vector内部实现中,使用动态的数组分配存储元素,重新分配数组时需要将原来所有的元素重新分配,移动到新数组中。对于预先不知道数组开多大的情况,可以使用vector。
头文件:<vector>
定义:vector<typename> name
常用方法:
状态 | |
empty() | 返回是不是空的 |
size() | 返回向量元素个数 |
添加或删除 | |
push_back() | 向尾部添加元素 |
pop_back() | 删除尾部元素 |
元素操作 | |
insert(index,num) | 指定位置index插入元素 |
inset(index,typesize,num) | 在index位置插入typesize个元素 |
元素访问 | |
begin() | 返回第一个元素的位置 |
end() | 最后一个元素的位置 |
front() | 返回a的第一个元素 |
back() | 返回a的最后一个元素 |
可以使用迭代器访问
for(vector<int>::iterator it=c.begin();it<c.end();it++)
print("%d",*it)
三、数据结构——队列
队列(queue),队列头部删除,队列尾删除
头文件:<queue>
定义:queue<typename> name
常用方法:
size() | 元素个数 |
empty() | 队列是否空 |
push() | 队尾入队 |
pop() | 队头出队 |
front() | 获得队头元素 |
back() | 获得队尾元素 |
例题:
三、数据结构——栈
数据从一头进,同一端出
头文件:<stack>
定义:stack<typename> name
方法:
size() | 返回个数 |
empty() | 返回是否空 |
push() | 添加新元素 |
pop() | 弹栈 |
top() | 只能通过栈顶访问元素 |