find
std::find
InputIterator find (InputIterator first, InputIterator last, const T& val); //
[first,last)
左闭右开比配的是vector中的值
string::find
(1)size_t find (const string& str, size_t pos = 0) const; //查找对象--string类对象
(2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串
(3)size_t find (const char* s, size_t pos, size_t n) const; //查找对象--字符串的前n个字符
(4)size_t find (char c, size_t pos = 0) const; //查找对象--字符
比配的是串或者字符
C++针对STL容器自定义compare准则
Sort函数:
sort(vec.begin(), vec.end(), cmp) ; //注意cmp返回值bool类型
写在类内:(重载<和仿函数)
struct Node{ int x, y; Node(int x,int y):x(x),y(y){} // 1.重载"<"运算符(写在类内部) bool operator<(const Node& a)const { // 返回true,表示this的优先级小于a // x大的排在前面;x相同时,y大的排在前面 if (x_ == a.x_) return y_ > a.y_; return x_ > a.x_; } // 2.重写仿函数(写在类内部) bool operator()(Node& a, Node& b) const { // 返回true,表示this的优先级小于a // x大的排在前面;x相同时,y大的排在前面 if (a.get_x() == b.get_x()) return a.get_y() > b.get_y(); return a.get_x() > b.get_x(); } };
sort(vec.begin(), vec.end()); //类内重载<
sort(vec.begin(), vec.end(),Node()); //类内重写仿函数(重载())
写在类外:
重载 < :
bool operator<(Node& a, Node& b) { // 返回true,表示this的优先级小于a // x大的排在前面;x相同时,y大的排在前面 if (a.get_x() == b.get_x()) return a.get_y() > b.get_y(); return a.get_x() > b.get_x(); }
sort(vec.begin(), vec.end(), cmp) ; //注意cmp返回值bool类型
仿函数类:
struct cmp{ bool operator()(node a, node b){ if (a.x == b.x) return a.y >= b.y; else return a.x > b.x; } };
priority_queue
模板声明带有三个参数,priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式,且Container 必须是用数组实现的容器STL里面默认用的是 vector. 比较方式默认用 operator<
使用: priority_queue<node> pq //类外重载< 【双参数】
struct node{ int x,y; }; bool operator < (node a, node b){ return true; } bool operator < (const node &a,const node &b){ return true; } bool operator < (const node a,const node b){ return true; } bool operator < (node &a, node &b){ //此写法错误 return true; }
使用: priority_queue<node> pq // 类内重载<【单参数】,但是后面必须加 const 修饰
同样const node &a和node a都可以,但node &a也不可以
struct node{ int x,y; bool operator<(const node &b) const{//注意后面的const必须加上 return true; } };
使用: priority_queue<node> pq //类内使用友元 【双参数】 ,但是后面不加 const 修饰,加了出错
同样const node &a和node a都可以,但node &a也不可以
struct node{ int x,y; friend bool operator<(const node&a, const node &b) {//注意后面的const不能加上 return true; } };
使用:priority_queue<node,vector<node>, cmp> pq; //自定义cmp类,类内重载 ( )
priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果,有第三个参数的话,必定要写第二个参数。
struct cmp{ bool operator()(const node &a, const node &b){ return true; } }
类里node&a,const node &a和node a都可以了,重载bool operator() 最后加const修饰也可以。
C++ 花式写法:
struct node {
int val;
ListNode* p;
node():val(0),p(nullptr){}
node(int val) :val(val), p(nullptr) {}
node(int val, ListNode* p1) :val(val), p(p1) {}
bool operator <(const node& a) {
return val > a.val;
}
};
int main(void) {
ListNode* p=new ListNode(1,nullptr);
node a = { p->val,p }; //神奇 { }
return 0;
}
C++ int最大数和最小数
# include<climits>
INT_MAX INT_MIN
vector的二维数组的定义:
vector < vector <int> > dp ( n , vector < int > (n)); //dp[n][n]
针对于C++字符串中的两种截取用法:
substr :返回一个从指定位置开始的指定长度的子字符串
substr(start , [length ]) ;//如果 length 为 0 或负数,将返回一个空字符串。
注意:如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
substring:方法将返回一个包含从 start 到最后(不包含 end )的子字符串的字符串。
substring(start, end)
注意:substring 方法使用 start 和 end 两者中的较小值作为子字符串的起始点。
例如, strvar.substring(0, 3) 和strvar.substring(3, 0) 将返回相同的子字符串。
如果 start 或 end 为 NaN 或者负数,那么将其替换为0。
子字符串的长度等于 start 和 end 之差的绝对值。
针对C++字符串拼接的方法:
(1)strcat(str3, str2); //把str3和str2拼接在一起并赋值给str3
(2)重载+,str3=str3+str2;
(3)append函数常用的三个功能:
- 直接添加另一个完整的字符串:如str1.append(str2);
- 添加另一个字符串的某一段子串:如str1.append(str2, 11, 7);//str2的第11个字符包含,往后共7个字母,追加至str1
- 添加几个相同的字符:如str1.append(5, ‘.’);注意,个数在前字符在后.上面的代码意思为在str1后面添加5个"."
针对C++ 长度函数:
length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.
strlen()和size()有什么区别?
char str[]=“1234abcd\0XYZ”;
strlen(str) 得 8, 不计 结束符 \0 和它以后的 字节。
sizeof(str) 得 12,计入 结束符 \0 和它以后的 字节
size() 功能 通常 类似 sizeof()
把string转换为char* 有 3种方法:
1. 调用 string 的 data 函数
如: string str="abc";
char *p=str.data();
2.调用 string 的 c_str 函数如:string str="gdfd";
char *p=str.c_str();c_str是标准的做法,返回的char* 一定指向一个合法的用'\0'终止的C兼容的字符串。
所以,如果需要C兼容的字符串,c_str 是标准的做法,data 并不保证所有STL的实现的一致性。3 调用 string 的 copy 函数
如: string str="hello";
char p[40];
str.copy(p,5,0); //这里5,代表复制几个字符,0代表复制的位置
*(p+5)='/0'; //要手动加上结束符
cout < <p;
C++ 字符串相等判断:
strcmp(str1, str2) == 0(成立则两个字符du串相等)
该函数返回值如下:
- 如果返回值小于 0,则表示 str1 小于 str2。
- 如果返回值大于 0,则表示 str1 大于 str2。
- 如果返回值等于 0,则表示 str1 等于 str2。
string.compare(str2) 等于0相等,否则按照字典排序比返回正负值
string s("Abdeas");
string n("Abdeas");
cout<<"s.compare(1,2,n)="<<s.compare(1,2,n)<<endl;//bd与Abdeas相比
cout<<"s.compare(1,2,n,1,3)="<<s.compare(1,2,n,1,3)<<endl;//bd与bde相
Vector中移除重复元素
#include <algorithm>
unique(nums.begin(), nums.end());//重复元素甩最后,返回的是最后一个非重复元素的后一个地址
但是要记住:要先排序才能用
sort(nums.begin(), nums.end());
unique(nums.begin(), nums.end())
size=unique(nums.begin(), nums.end()) - nums.begin();
Vector中删除元素
remove(v.begin(), v.end(), 99); //remove有返回值 , newEnd位置
iterator erase (iterator position);
iterator erase (iterator first, iterator last);//这个删除的范围是左闭右开
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
有限队列中自写compare准则的两种方法:
struct node
{
int to,cost;
node(int x1,int x2)
{
to=x1;
cost = x2;
}
friend bool operator<(const node &a , const node &b)
{
return a.cost>b.cost; // ascending sort
}
};
priority_queue<node>priq;
struct node
{
int to,cost;
node(int x1,int x2)
{
to=x1;
cost = x2;
}
friend bool operator<(const node &a , const node &b)
{
return a.cost>b.cost; // ascending sort
}
};
priority_queue<node>priq;