C++集合
Vector
push_back & pop_back
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>obj;//创建一个向量存储容器 int
for(int i=0;i<10;i++) // push_back(elem)在数组最后添加数据
{
obj.push_back(i);
cout<<obj[i]<<",";
}
for(int i=0;i<5;i++)//去掉数组最后一个数据
{
obj.pop_back();
}
cout<<"\n"<<endl;
for(int i=0;i<obj.size();i++)//size()容器中实际数据个数
{
cout<<obj[i]<<",";
}
return 0;
}
clear
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>obj;
for(int i=0;i<10;i++)//push_back(elem)在数组最后添加数据
{
obj.push_back(i);
cout<<obj[i]<<",";
}
obj.clear();//清除容器中所以数据
for(int i=0;i<obj.size();i++)
{
cout<<obj[i]<<endl;
}
return 0;
}
algorithem/sort/reverse
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int>obj;
obj.push_back(1);
obj.push_back(3);
obj.push_back(0);
sort(obj.begin(),obj.end());//从小到大
cout<<"从小到大:"<<endl;
for(int i=0;i<obj.size();i++)
{
cout<<obj[i]<<",";
}
cout<<"\n"<<endl;
cout<<"从大到小:"<<endl;
reverse(obj.begin(),obj.end());//从大到小
for(int i=0;i<obj.size();i++)
{
cout<<obj[i]<<",";
}
return 0;
}
重写sort
bool com(int a , int b)
{
return a< b; //升序排列,如果改为return a>b,则为降序
}
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++)
cout<< a[i]<< endl;
sort(a,a+20,compare);
访问
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//顺序访问
vector<int>obj;
for(int i=0;i<10;i++)
{
obj.push_back(i);
}
cout<<"直接利用数组:";
for(int i=0;i<10;i++)//方法一
{
cout<<obj[i]<<" ";
}
cout<<endl;
cout<<"利用迭代器:" ;
//方法二,使用迭代器将容器中数据输出
vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
for(it=obj.begin();it!=obj.end();it++)
{
cout<<*it<<" ";
}
return 0;
}
用vector定义一个二维数组
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N=5, M=6;
vector<vector<int> > obj(N); //定义二维动态数组大小5行
for(int i =0; i< obj.size(); i++)//动态二维数组为5行6列,值全为0
{
// 分配内存,注意这里可以传两个参数,第一个是大小,第二个是初始值
obj[i].resize(M);
}
for(int i=0; i< obj.size(); i++)//输出二维动态数组
{
for(int j=0;j<obj[i].size();j++)
{
cout<<obj[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N=5, M=6;
vector<vector<int> > obj(N, vector<int>(M)); //定义二维动态数组5行6列
for(int i=0; i< obj.size(); i++)//输出二维动态数组
{
for(int j=0;j<obj[i].size();j++)
{
cout<<obj[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
max_element
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v;
v.push_back(7); v.push_back(4);
v.push_back(2); v.push_back(9);
cout << *max_element(v.begin(), v.end()) << endl;//9
cout << *min_element(v.begin(), v.end()) << endl;//2
return 0;
}
获取最大值的index
#include <iostream> // std::cout
#include <iterator> // std::distance
#include <list> // std::list
#include <algorithm>
using namespace std ;
int main () {
std::list<int> mylist;
for (int i=0; i<10; i++) mylist.push_back (i*10);
std::list<int>::iterator first = mylist.begin();
std::list<int>::iterator last = mylist.end();
// cout << *(mylist.begin()) ;
// int indexOfMax= max_element(mylist.begin(),mylist.end()) - mylist.begin();
list<int>::iterator it = max_element(mylist.begin(),mylist.end()) ;
int max_index = distance(mylist.begin(),it) ;
cout << max_index ;
// std::cout << "The distance is: " << std::distance(first,last) << '\n';
return 0;
}
count
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std ;
int main()
{
vector<int> obj(10,1) ;
int count1 = count(obj.begin() , obj.end() , 1) ;
cout << count1 << endl ;
return 0 ;
}
String
构造函数
string s1(); // si = ""
string s2("Hello"); // s2 = "Hello"
string s3(4, 'K'); // s3 = "KKKK"
string s4("12345", 1, 3); //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串
// 这两种写法是错误的
string s1('K');
string s2(123);
assign函数
string s1("12345") , s2 ;
s2.assign(s1) ;
s2.assign(s1,1,3) ;
s2.assign(4,'s') ;
s2.assign("abcde", 2, 3); // s2 = "cde",即 "abcde" 的子串(2, 3)
length函数
string s1("12345");
s1.length ;
append函数
string s1("123"), s2("abc");
s1.append(s2); // s1 = "123abc"
s1.append(s2, 1, 2); // s1 = "123abcbc"
s1.append(3, 'K'); // s1 = "123abcbcKKK"
s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)
string对象的比较
/*
小于 0 表示当前的字符串小;
等于 0 表示两个字符串相等;
大于 0 表示另一个字符串小。
*/
string s1("hello"), s2("hello, world");
int n = s1.compare(s2);
n = s1.compare(1, 2, s2, 0, 3); //比较s1的子串 (1,2) 和s2的子串 (0,3)
n = s1.compare(0, 2, s2); // 比较s1的子串 (0,2) 和 s2
n = s1.compare("Hello");
n = s1.compare(1, 2, "Hello"); //比较 s1 的子串(1,2)和"Hello”
n = s1.compare(1, 2, "Hello", 1, 2); //比较 s1 的子串(1,2)和 "Hello" 的子串(1,2)
求 string 对象的子串
substr 成员函数可以用于求子串 (n, m),原型如下:
string substr(int n = 0, int m = string::npos) const;
调用时,如果省略 m 或 m 超过了字符串的长度,则求出来的子串就是从下标 n 开始一直到字符串结束的部分。例如:
string s1 = "this is ok";
string s2 = s1.substr(2, 4); // s2 = "is i"
s2 = s1.substr(2); // s2 = "is is ok"
交换两个string对象的内容
string s1("West”), s2("East");
s1.swap(s2); // s1 = "East",s2 = "West"
查找子串和字符
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("Source Code");
int n;
if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
cout << "1) " << n << "," << s1.substr(n) << endl;
//输出 l)2,urce Code
if ((n = s1.find("Source", 3)) == string::npos)
//从下标3开始查找"Source",找不到
cout << "2) " << "Not Found" << endl; //输出 2) Not Found
if ((n = s1.find("Co")) != string::npos)
//查找子串"Co"。能找到,返回"Co"的位置
cout << "3) " << n << ", " << s1.substr(n) << endl;
//输出 3) 7, Code
if ((n = s1.find_first_of("ceo")) != string::npos)
//查找第一次出现或 'c'、'e'或'o'的位置
cout << "4) " << n << ", " << s1.substr(n) << endl;
//输出 4) l, ource Code
if ((n = s1.find_last_of('e')) != string::npos)
//查找最后一个 'e' 的位置
cout << "5) " << n << ", " << s1.substr(n) << endl; //输出 5) 10, e
if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
//从下标1开始查找第一次出现非 'e'、'o' 或 'u' 字符的位置
cout << "6) " << n << ", " << s1.substr(n) << endl;
//输出 6) 3, rce Code
return 0;
}
替换子串
string s1("Real Steel");
s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替换 s1 的子串(1,3)
cout << s1 << endl; //输出 R3456 Steel
string s2("Harry Potter");
s2.replace(2, 3, 5, '0'); //用 5 个 '0' 替换子串(2,3)
cout << s2 << endl; //输出 HaOOOOO Potter
int n = s2.find("OOOOO"); //查找子串 "00000" 的位置,n=2
s2.replace(n, 5, "XXX"); //将子串(n,5)替换为"XXX"
cout << s2 < < endl; //输出 HaXXX Potter
删除子串
string s1("Real Steel");
s1.erase(1, 3); //删除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5); //删除下标5及其后面的所有字符,此后 s1 = "R Ste"
插入字符串
string s1("Limitless"), s2("00");
s1.insert(2, "123"); //在下标 2 处插入字符串"123",s1 = "Li123mitless"
s1.insert(3, s2); //在下标 2 处插入 s2 , s1 = "Li10023mitless"
s1.insert(3, 5, 'X'); //在下标 3 处插入 5 个 'X',s1 = "Li1XXXXX0023mitless"
用STL操作String
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s("afgcbed");
string::iterator p = find(s.begin(), s.end(), 'c');
if (p!= s.end())
cout << p - s.begin() << endl; //输出 3
sort(s.begin(), s.end());
cout << s << endl; //输出 abcdefg
next_permutation(s.begin(), s.end());
cout << s << endl; //输出 abcdegf
return 0;
}
tolower &toupper
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std ;
int main()
{
vector<string> obj(2);
// vector<string>::iterator it = obj.begin() ;
obj[1] = "abcd";
obj[0] = "efgh" ;
// obj[0].insert(obj[0].find("4"),"1") ;
// cout << obj[0] <<endl;
for (int i = 0 ; i < obj.size() ; ++i)
{
// cout << obj[i] <<endl ;
for (string::iterator it = obj[i].begin() ; it != obj[i].end() ; ++it)
{
*it = toupper(*it) ;
}
}
cout << obj[0] << endl ;
cout << obj[1] << endl ;
return 0 ;
}
list
list初始化
同vector类似
push_back & push_front
listOne.push_front(3);
listOne.push_front(2);
listOne.push_front(1);
listOne.push_back(4);
listOne.push_back(5);
listOne.push_back(6);
list.front() & list.back()
LISTINT a;
a.push_back(1) ;
a.push_front(5) ;
cout << "first one" <<a.front() << endl;
cout << "last one" << a.back() << endl;
sort
Lst1.sort()
insert
在第一个位置前面添加5个10
a.insert(++a.begin(),5,10) ;
a.insert(lstA.begin() , lstB.begin() , lstB.end() );
erase
list<int>::iterator it1=L.begin();
list<int>::iterator it2=L.begin();
it2++;
it2++;
//左开右闭
L.erase(it1,it2);
remove
L.remove(elem);
/*
比如链表中元素如下:12343333744323333
要删除链表中3元素
*/
L.remove(3);
clear
list.clear(); //移除容器的所有数据
rbegin & rend
得到最后一个元素的位置 end-1
rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
reverse
// 反转
list.reverse()