1. 读取数量不定的输入数据
#include<iostream>
using namespace std;
int main()
{
int sum = 0, value = 0;
while (std::cin>>value) //读入一个数据,执行一次循环
{
sum += value;
}
std::cout << "Sum is :" << sum << std::endl;
return 0;
}
此循环的条件是std::cin
,当我们使用一个istream
对象作为条件时,其效果是检查流的状态。如果流是有效的,即流未遇到错误,那么检测成功。当遇到文件结束符,或者无效输入时(例如读入的不是一个整数),istream
对象的状态会变为无效
。处于无效
状态的istream
对象会使条件变为假
。
2. 引用
int a=0;
int &b=a;//注意写法
3. vector
3.1 列表初始化
- int 类型
vector<int> v1(10);//v1有10个元素,每一个元素的值都是0;
vector<int> v2{10};//v2有一个元素,这个元素是10
/***************注意:()与{}之间的含义***************/
vector<int> v3(10,1);//v3有10个元素,每一个元素的值都是1;
vector<int> v4{10,1};//v4有两个元素,分别是10与1;
- string 类型
vector<string> v5{"hi"};//初始化列表只有一个元素;
vector<string> v6("hi");//**错误**//
/****************string不使用()初始化*******************/
vector<string> v7{10};//v7有10个默认初始化的元素;
vector<string> v8{10,"hi"};//v8有10个值为"hi"的元素;
3.2 通过下标运算符[ ]随机访问
vector(对[ ]符号进行重载的容器均可以使用随机访问/修改/或者读取)
vector<unsigned> scores(11 ,0);//11个分数段,全部初始化为0;
unsigned grade;
while(cin>>grade)
{
if(grade<=100)
{
++scores[grade/10]; //对应分数段的计数+1;
}
}
3.3 不能使用下标运算符[ ]添加元素
若想在vector中添加元素,不能通过下标运算符进行,可以通过push_back()或者insert()等成员函数;
3.4 迭代器类型
迭代器按照定义方式分成以下四种。
- 正向迭代器,定义方法如下:
容器类名::iterator 迭代器名;
- 常量正向迭代器,定义方法如下:
容器类名::const_iterator 迭代器名;
- 反向迭代器,定义方法如下:
容器类名::reverse_iterator 迭代器名;
- 常量反向迭代器,定义方法如下:
容器类名::const_reverse_iterator 迭代器名;
- string类型迭代器:
vector<int>::iterator ite;//ite能读写vector<int>的元素;
string::iterator ite2;//ite2能够读写string对象中的字符
vector<int>::const_iterator ite3;//ite3只能读取元素,不能写元素
string::const_iterator ite4;//ite4只能读字符不能写字符
3.5 迭代器运算
通过迭代器运算可以使迭代器每次移动跨过多个元素;
vector<int> ite1,ite2;
方法 | 功能 |
---|---|
ite1+n | 迭代器加上一个整数值仍然是一个迭代器,迭代器指示的新位置与原来相比向前移动了n. |
ite1-n | 迭代器减上一个整数值仍然是一个迭代器,迭代器指示的新位置与原来相比向后移动了n。 |
ite1-ite2 | 两个迭代器相减的结果是他们之间的距离 |
4. string 类型
4.1 string 类型特性
成员函数 | 功能 |
---|---|
Swap | 交换两个字符串的内容 |
insert () | 插入字符 |
erase() | 删除字符 |
clear () | 移除全部字符 |
resize () | 改变字符数量 |
replace() | 替换字符 |
+ | 串联字符串 |
+ ==,! =,<,<=,>,>=,compare() | 比较字符串内容 |
size(),length() | 返回字符数量 |
max_size () | 返回字符的最大可能个数 |
empty () | 判断字符串是否为空 |
capacity () | 返回重新分配之前的字符容量 |
reserve() | 保留内存以存储一定数量的字符 |
[],at() | 存取单一字符at() |
>>,getline() | 从 stream 中读取某值 |
<< | 将值写入 stream |
copy() | 将内容复制为一个 C - string |
c_str() | 将内容以 C - string 形式返回 |
data() | 将内容以字符数组形式返回 |
substr() | 返回子字符串 |
find() | 搜寻某子字符串或字符 |
begin( ),end() | 提供正向迭代器支持 |
rbegin(),rend() | 提供逆向迭代器支持 |
get_allocator() | 返回配置器 |
要想使用标准C++中string类,必须要包含
#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件
using std::string;
using std::wstring;
或
using namespace std;
下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。
string和wstring的用法是一样的,以下只用string作介绍:
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相似,只不过是从后向前查找
4.2 使用范围for
4.2.1 使用范围for处理每一个字符
@brief;遍历给定序列中的每一元素并对序列中的每个值执行某种操作;
@param:declaration 定义一个变量,该变量将被用于访问序列中的基础元素。
@param:expression部分是一个对象,用与表示一个序列;
每一次迭代,declaration部分的变量会被初始化为expression部分的下一个元素值。
for(declaration : expression)
statement
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("hello world!");
for(auto c:str) //每一次迭代,str的下一个字符被拷贝给C。
{
cout<<c<<" ";
}
cout<<endl;
return 0;
}
4.2.2 使用范围for改变字符串中字符
如果想改变string对象中字符的值,必须将循环变量定义成引用类型
string str("hello world!");
for(auto &c:str) //c是一个引用,所以可以改变str中字符的值
{
c=toupper(c);
}
4.2.3 使用范围for改变字符串中部分字符
如果需要改变字符串中部分的字符串,可以使用下标运算符[ ];使用下标运算符可以执行随机访问。
string str("hello world!");
if(!str.empty())
{
cout<<str[0]<<endl;
char c=toupper(str[0]);
cout<<c<<endl;
}
4.2.4 string 迭代器string::iterator
5. 标准库函数begin和end
尽管能计算得到尾后指针,但是这种做法极易出错。
int ia[]={0,1,2,3,4,5,6,7,8,9};
int *beg=begin(ia); //指向ia首元素的指针;
int *last=end(ia); //指向ia尾元素的下一位置的指针;
vector<int> via(begin(ia),end(ia));
vector<int> via_(ia,ia+10);