1.set创建字符串数组:
set 不能用来计数,只能用来判断是否存在这个string。
#include <set>
set<string> str;
#添加数值
str.insert(str_new);
#set 默认是从小到大排序
#两个int插入用法,按照第一个数值排序。
set<pair<int,int> > s;
#访问第一个值
s.begin()
#如果是双int
int start,end;
start=(*s.begin()).first;
end=(*s.begin()).second;
#访问最后一个;
set<pair<int,int> >::iterator iter;
iter=s.end();
iter--;
#注意 不能用s.end()--,会出现错误,必须分开操作
使用set 可以很好的创建字符串数组;
这是他的方法:
1. begin()--返回指向第一个元素的迭代器
2. clear()--清除所有元素
3. count()--返回某个值元素的个数
4. empty()--如果集合为空,返回true
5. end()--返回指向最后一个元素的迭代器
6. equal_range()--返回集合中与给定值相等的上下限的两个迭代器
7. erase()--删除集合中的元素
8. find()--返回一个指向被查找到元素的迭代器
9. get_allocator()--返回集合的分配器
10. insert()--在集合中插入元素
11. lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器
12. key_comp()--返回一个用于元素间值比较的函数
13. max_size()--返回集合能容纳的元素的最大限值
14. rbegin()--返回指向集合中最后一个元素的反向迭代器
15. rend()--返回指向集合中第一个元素的反向迭代器
16. size()--集合中元素的数目
17. swap()--交换两个集合变量
18. upper_bound()--返回大于某个值元素的迭代器
19. value_comp()--返回一个用于比较元素间的值的函数
使用
str.count(ss)
可以判断是否存在这个字符串,来避免重复。
对字符串的遍历:
set<string>::iterator iter=str.begin();
while(iter!=str.end())
{
cout<<*iter<<endl;
++iter;
}
最后,set里面字符串已经按字典序排序过了,所以不需要再排序。
2.string的一些方法
下面转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html
要想使用标准C++中string类,必须要包含
#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件
string类的构造函数:
string(const char *s); //用c字符串s初始化
string(int n,char c); //用n个字符c初始化
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ;
string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
string的特性描述:
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const; //返回string对象中可存放的最大字符串的长度
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分
string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s); //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c); //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中
//pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0
string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
string的交换:
void swap(string &s2); //交换当前字符串与s2的值
string类的查找函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
string类的插入函数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
string类的删除函数
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
string类的迭代器处理:
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend(); //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现
下面转自:https://www.runoob.com/cplusplus/cpp-strings.html
序号 | 函数 & 目的 |
---|---|
1 | strcpy(s1, s2); 复制字符串 s2 到字符串 s1。 |
2 | strcat(s1, s2); 连接字符串 s2 到字符串 s1 的末尾。 |
3 | strlen(s1); 返回字符串 s1 的长度。 |
4 | strcmp(s1, s2); 如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0。 |
5 | strchr(s1, ch); 返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。 |
6 | strstr(s1, s2); 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。 |
3.map使用
参考:https://blog.youkuaiyun.com/ajianyingxiaoqinghan/article/details/78540736
一、原型
-
template < class Key, // map::key_type
-
class T, // map::mapped_type
-
class Compare = less<Key>, // map::key_compare
-
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
-
> class map;
二、说明
map 是一种容器,用来存储若干元素,这些元素都是由关键值 (Key Value,以下称为 Key 值) 和映射值 (Mapped Value,以下依旧称为映射值) 配对组成的,具体说明如下:
在一个 map 中, Key 值通常用来排序或特指元素,映射值用来存储与该 Key 值绑定的内容。 Key 值与映射值的数据类型可以不同,而且可以一起被放进成员类型 value_type 中,value_type 是一种配对类型,定义如下:
typedef pair<const Key, T> value_type;
在 map 内部的元素通常按照其 Key 值排序,且排序方式是根据某种明确、严格的弱排序标准进行的,这种排序标准是由 map 内部的比较对象(即 map::key_comp)指定的。
map 容器通过 Key 值访问特定元素的速度,相较于 unordered_map 容器通常较慢,但 map 容器允许基于它们的顺序对子集进行直接迭代。
map 中的映射值可以使用括号运算符 (operator[]) 通过其关联的 Key 值直接访问。
map 通常使用二叉搜索树实现。
三、map 容器属性
关联性:
关联容器中的元素的参考地址指的是其 Key 值,而不是他们在容器中的绝对地址;
有序性:
容器中的元素一直按照排序方式严格排序,所有插入元素都按照该顺序排列;
映射:
每个元素中,一个 Key 值与一个映射值相关。Key 值是用来标识其主要内容是映射值的元素;
唯一 Key 值:
容器中不存在同时拥有相同 Key 值的两个元素;
分配感知 (Allocator-aware):
map 容器使用分配器对象动态处理其存储需求。
四、模板参数
Key
Key 值的类型。在 map 中的每个元素都是由其 Key 值唯一指定的。
别名为成员类型 map::key_type
T
映射值的类型。在 map 中的每个元素,都存储了一些数据作为其映射值。
别名为成员类型 map::mapped_type
Compare
一个二元值,它将两个元素的 Key 值作为输入参数,并返回一个布尔值。表达式 comp(a, b),其中 comp 是该类型的对象,a, b是 Key 值,如果按照函数定义的严格弱排序,参数 a 被认为排在参数 b 之前,则返回 true。
map 对象使用该表达式确定元素在容器中的位置,并判断两个元素的 Key 值是否相等(通过自反比较:如果 (!comp(a,b) && !comp(b,a) ) 结果为真,则 a, b 等价)。map 容器中没有两个元素拥有相同的 Key 值。
Compare 可以使一个函数指针,或者函数对象(详细请参阅示例构造函数)。默认值小于<T>,返回应用小于运算符 (a < b) 相同的值;
别名为成员类型 map::key_compare
Alloc
用于定义存储分配模型的分配器对象的类型。默认情况下使用分配器类模板,它定义了最简单的模型分配模型,而且与值无关。
别名为成员类型 map::allocator_type
五、常用函数
构造函数
在后续的程序示例中展示了五种不同的构造函数;
clear
清除 map 中所有元素;
erase
删除 map 中指定位置的元素;
insert
在 map 指定位置添加 pair 类型的元素;
find
获取 map 中元素的迭代器;
begin, end
map 的正向迭代器的起始位置与终点位置;
rbegin, rend
map 的反向迭代器的起始位置与终点位置;
六、程序示例
以下源码摘自《C++STL之map学习》,笔者对其进行注释。
#include <iostream>
#include <map>
using namespace std;
// 比较函数(用于后面的函数指针定义)
bool fncomp(char lhs, char rhs){
return lhs < rhs;
}
// 定义一个 Compare 对象,且内部对运算符 () 进行重载
struct classcomp{
bool operator() (const char& lhs, const char& rhs){
return lhs < rhs;
}
};
int main(int argc, char* argv[])
{
//=========================
// map 的多种构造函数
//=========================
// 1. 直接定义
map<char,int> mymap;
mymap['a'] = 10;
mymap['b'] = 60;
mymap['c'] = 30;
mymap['d'] = 90;
mymap['e'] = 50;
// 2. 复制
map<char, int> second(mymap);
// 3. 通过迭代器
map<char, int> third(mymap.begin(),mymap.end());
// 4. 重新定义 Compare 对象,该对象内部对运算符 () 进行重载
map<char, int, classcomp> fourth;
// 5. 通过函数指针
bool(*fn_pt)(char, char) = fncomp;
map<char, int, bool(*)(char, char)> fifth(fn_pt);
//=========================
// 以最初定义的mymap 为例,讲解 map 的使用
//=========================
map<char,int>::key_compare key_comp;
map<char,int>::iterator it;
it = mymap.begin();
//=========================
// 1. 输出所有 Pair 元素
//=========================
// 迭代器遍历 map
for (it; it != mymap.end(); it++)
{
// map的迭代器,可以用 first 访问std::pair的第一个成员(Type1),second 访问第二个成员 (Type2)
cout<<it->first<<":"<<it->second<<endl;
}
cout<<"================================="<<endl;
//=========================
// 2. 修改映射值
//=========================
second.clear();
second['a']=1002;
second['b']=10023;
while (!second.empty())
{
cout << second.begin()->first << " => ";
cout << second.begin()->second << endl;
second.erase(second.begin());
}
cout<<"================================="<<endl;
//=========================
// 3. 插入
//=========================
mymap.insert(pair<char,int>('f',100) );
mymap.insert(pair<char,int>('g',200) );
cout<<"f => " <<mymap.find('f')->second<<endl;
cout<<"g => " <<mymap.find('g')->second<<endl;
cout<<"================================="<<endl;
//=========================
// 4. Compare 参数的使用
//=========================
key_comp = mymap.key_comp();
cout << "mymap contains:\n";
// 迭代器反向遍历的起始位置
char highest = mymap.rbegin()->first; // key value of last element
it = mymap.begin();
do {
cout << (*it).first << " => " << (*it).second << endl;
} while ( key_comp((*it++).first, highest) );
cout << endl;
return 0;
}