字符串,数组,向量

标准库类型string

#include<string>

string 定义在命名空间std中

  1. //string函数用法详解!附代码,写具体的用法!   
  2. #include <iostream>  
  3. #include <string>  
  4. #include <sstream>   
  5. using namespace std;  
  6.   
  7.   
  8. int main()  
  9. {  
  10.     //1.string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作  
  11.     string str1;  
  12.     cin >> str1;//当用cin>>进行字符串的输入的时候,遇到空格的地方就停止字符串的读取输入   
  13.     cout << str1 << endl;  
  14.     cin.get();//这个的作用就是读取cin>>输入的结束符,不用对getline的输入产生影响!   
  15.     getline(cin, str1);//字符串的行输入  
  16.     cout << str1 << endl;   
  17.       
  18.       
  19.     //2.string类的构造函数   
  20.     string str2 = "aaaaa";//最简单的字符串初始化   
  21.     cout << str2 << endl;   
  22.       
  23.     char *s = "bbbbb";  
  24.     string str3(s);//用c字符串s初始化   
  25.     cout << str3 << endl;  
  26.       
  27.     char ch = 'c';  
  28.     string str4(5, ch);//用n个字符ch初始化   
  29.     cout << str4 << endl;   
  30.       
  31.     //3.string类的字符操作  
  32.     string str5 = "abcde";   
  33.     ch = str5[3];//operator[]返回当前字符串中第n个字符的位置   
  34.     cout << ch << endl;   
  35.       
  36.     string str6 = "abcde";  
  37.     ch = str6.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!    
  38.     cout << ch << endl;   
  39.       
  40.     //4.string的特性描述  
  41.     string str7 = "abcdefgh";  
  42.     int size;  
  43.     size = str7.capacity();//返回当前容量   
  44.     cout << size << endl;   
  45.     size = str7.max_size();//返回string对象中可存放的最大字符串的长度   
  46.     cout << size << endl;   
  47.     size = str7.size();//返回当前字符串的大小   
  48.     cout << size << endl;   
  49.     size = str7.length();//返回当前字符串的长度   
  50.     cout << size << endl;   
  51.     bool flag;  
  52.     flag = str7.empty();//判断当前字符串是否为空   
  53.     cout << flag << endl;  
  54.     int len = 10;   
  55.     str7.resize(len, ch);//把字符串当前大小置为len,并用字符ch填充不足的部分   
  56.     cout << str7 << endl;   
  57.       
  58.     //5.string的赋值  
  59.     string str8;  
  60.     str8 = str7;//把字符串str7赋给当前字符串  
  61.     cout << str8 << endl;  
  62.     str8.assign(str7);//把字符串str7赋给当前字符串   
  63.     cout << str8 << endl;   
  64.     str8.assign(s);//用c类型字符串s赋值   
  65.     cout << str8 << endl;   
  66.     str8.assign(s, 2);//用c类型字符串s开始的n个字符赋值   
  67.     cout << str8 << endl;   
  68.     str8.assign(len, ch);//用len个字符ch赋值给当前字符串   
  69.     cout << str8 << endl;   
  70.     str8.assign(str7, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串   
  71.     cout << str8 << endl;   
  72.     string str9 = "0123456789";  
  73.     str8.assign(str9.begin(), str9.end());//把迭代器之间的字符赋给字符串   
  74.     cout << str8 << endl;   
  75.       
  76.     //6.string的连接  
  77.     string str10;  
  78.     str10 += str9;//把字符串str9连接到当前字符串的结尾   
  79.     cout << str10 << endl;  
  80.     str10.append(s);//把c类型字符串s连接到当前字符串的结尾   
  81.     cout << str10 << endl;   
  82.     str10.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾   
  83.     cout << str10 << endl;   
  84.     str10.append(str9.begin(), str9.end());//把迭代器之间的一段字符连接到当前字符串的结尾   
  85.     cout << str10 << endl;   
  86.     str10.push_back('k');//把一个字符连接到当前字符串的结尾   
  87.     cout << str10 << endl;   
  88.       
  89.     //7.string的比较  
  90.     flag = (str9 == str10);//判断两个字符串是否相等   
  91.     cout << flag << endl;  
  92.     flag = (str9 != str10);//判断两个字符串是否不相等   
  93.     cout << flag << endl;   
  94.     flag = (str9 > str10);//判断两个字符串是否大于关系   
  95.     cout << flag << endl;  
  96.     flag = (str9 < str10);//判断两个字符串是否为小于关系   
  97.     cout << flag << endl;  
  98.     flag = (str9 >= str10);//判断两个字符串是否为大于等于关系   
  99.     cout << flag << endl;  
  100.     flag = (str9 <= str10);//判断两个字符串否为小于等于关系   
  101.     cout << flag << endl;   
  102.       
  103.     //以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0   
  104.     flag = str10.compare(str9);//比较两个字符串的大小,通过ASCII的相减得出!   
  105.     cout << flag << endl;   
  106.     flag = str10.compare(6, 12, str9);//比较str10字符串从6开始的12个字符组成的字符串与str9的大小   
  107.     cout << flag << endl;  
  108.     flag = str10.compare(6, 12, str9, 3, 5);//比较str10字符串从6开始的12个字符组成的字符串与str9字符串从3开始的5个字符组成的字符串的大小   
  109.     cout << flag << endl;   
  110.       
  111.     //8.string的字串  
  112.     string str11;  
  113.     str11 = str10.substr(10, 15);//返回从下标10开始的15个字符组成的字符串   
  114.     cout << str11 << endl;   
  115.       
  116.     //9.string的交换  
  117.     str11.swap(str10);//交换str11与str10的值   
  118.     cout << str11 << endl;   
  119.       
  120.     //10.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1   
  121.     string str12 = "abcdefghijklmnopqrstuvwxyz";  
  122.     int pos;  
  123.     pos = str12.find('i', 0);//从位置0开始查找字符i在当前字符串的位置   
  124.     cout << pos << endl;  
  125.     pos = str12.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置   
  126.     cout << pos << endl;   
  127.     pos = str12.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置   
  128.     cout << pos << endl;   
  129.     pos = str12.rfind('s', string::npos);//从字符串str12反向开始查找字符s在字符串中的位置   
  130.     cout << pos << endl;   
  131.     pos = str12.rfind("klmn", string::npos);//从字符串str12反向开始查找字符串“klmn”在字符串中的位置   
  132.     cout << pos << endl;  
  133.     pos = str12.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置   
  134.     cout << pos << endl;   
  135.       
  136.     string str13 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx";  
  137.     pos = str13.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置   
  138.     cout << pos << endl;   
  139.     pos = str13.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置   
  140.     cout << pos << endl;   
  141.     pos = str13.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置   
  142.     cout << pos << endl;  
  143.     pos = str13.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置   
  144.     cout << pos << endl;   
  145.     pos = str13.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置   
  146.     cout << pos << endl;   
  147.     pos = str13.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置   
  148.     cout << pos << endl;   
  149.     //下面的last的格式和first的一致,只是它从后面检索!   
  150.     pos = str13.find_last_of('b', string::npos);  
  151.     cout << pos << endl;  
  152.     pos = str13.find_last_of("abcdef", string::npos);  
  153.     cout << pos << endl;  
  154.     pos = str13.find_last_of("abcdef", string::npos, 2);  
  155.     cout << pos << endl;   
  156.     pos = str13.find_last_not_of('a', string::npos);  
  157.     cout << pos << endl;   
  158.     pos = str13.find_last_not_of("abcdef", string::npos);  
  159.     cout << pos << endl;  
  160.     pos = str13.find_last_not_of("abcdef", string::npos, 3);  
  161.     cout << pos << endl;  
  162.        
  163.     //11.string的替换   
  164.     string str14 = "abcdefghijklmn";  
  165.     str14.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq”   
  166.     cout << str14 << endl;   
  167.     str14.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符   
  168.     cout << str14 << endl;   
  169.     str14.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符   
  170.     cout << str14 << endl;   
  171.     str14.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c   
  172.     cout << str14 << endl;   
  173.     //上面的位置可以换为迭代器的位置,操作是一样的,在这里就不再重复了!   
  174.       
  175.     //12.string的插入,下面的位置处亦可以用迭代器的指针表示,操作是一样的   
  176.     string str15 = "abcdefg";  
  177.     str15.insert(0, "mnop");//在字符串的0位置开始处,插入字符串“mnop”   
  178.     cout << str15 << endl;   
  179.     str15.insert(0, 2, 'm');//在字符串的0位置开始处,插入2个字符m   
  180.     cout << str15 << endl;   
  181.     str15.insert(0, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符   
  182.     cout << str15 << endl;  
  183.     str15.insert(0, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符   
  184.     cout << str15 << endl;   
  185.       
  186.     //13.string的删除  
  187.     string str16 = "gfedcba";  
  188.     string::iterator it;  
  189.     it = str16.begin();  
  190.     it++;  
  191.     str16.erase(it);//删除it指向的字符,返回删除后迭代器的位置   
  192.     cout << str16 << endl;  
  193.     str16.erase(it, it+3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置   
  194.     cout << str16 << endl;   
  195.     str16.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符   
  196.     cout << str16 << endl;   
  197.       
  198.     //14.字符串的流处理  
  199.     string str17("hello,this is a test");  
  200.     istringstream is(str17);  
  201.     string s1,s2,s3,s4;  
  202.     is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"  
  203.     ostringstream os;  
  204.     os<<s1<<s2<<s3<<s4;  
  205.     cout<<os.str() << endl;  
  206.       
  207.     system("pause");  

 

 

#include <cctype>

cctype头文件中的函数

isalnum(c) 当c为字母或数字时为真

isalpha(c)  当c为字母时为真

iscntrl(c)  当c为控制字符时为真

isdigit(c)   当c为数字时为真

isgraph(c)    当c不是空格但可打印时为真

islower(c)    当c为小写字母时为真

isprint(c)    当c为可打印字符时为真(即c为空格或者c具有可视化形式)

ispunct(c)    当c为标点符时为真(即c不是控制符,数字,字母可打印空白的一种)

isspace(c)    当c是空白时为真(即c为空格,横向制表符,纵向制表符,回车符,换行符,进制符的一种)

isupper(c)    当c为大写字母是为真

isxdigit(c)    当c为十六进制数字时为真

tolower(c)    把c变成小写

toupper(c)    把c变成大写

·    

范围for循环

for(declaration : expression)

 

 

标准库类型vector

#include <vector>

using std:: vector;

基本形式:vector<type>name

:vector是模板而非类型

vector能容纳绝大多数类型的对象作为元素,但因为引用不是对象,所以不包含引用的vetcor

初始化vector

C++提供了几种不同的初始化方式

1.使用拷贝初始化(即使用=)

2.如果提供的是一个类内初始值则只能使用拷贝初始化或者使用花括号的形式初始化

3.(特殊)如果提供的是初始元素值的列表,则只能把初始值都放在花括号里进行列表初始化,而不能用圆括号

eg:  vector<string> v1{"ac","an","the"};    // 列表初始化

        vector<string> v2("ac","an","the")    //error

 列表初始值or元素数量

eg:    vetcor<int>v1(10);    //v1中有十个元素,每个值都为10

         vector<int>v2{10};    //v2中有一个元素,值为10

         vetcor<int>v3(10,6)   //v3中有10个6

         vector<int>v4{10,6}    //v4中有两个元素10和6

向vector里添加元素

eg:

vector<int>vint;

for(int i=1;i<=10;i++)

vint.push_back(i);

 

形式:name.push_back(value)

 

 

vector的相关操作

v.empty();

v.size();

v.capacity();

v.push_back(t);

v1=v2;

 

和数组,string一样vector对象下标也是从0开始的

 

利用范围for循环可以处理vector里的元素

eg:

    vector<int>vint;

for(int i=1;i<=6;i++)

    vint.push_back(i);

for(auto &i : vint)

    cout<<i<<" ";

cout<<endl;

 

 

vector不能用下标形式添加元素,只能对已知存在的元素进行下标操作

确保下标合法的一种有效操作就是使用范围for语句

 

迭代器(迭代器是一种检查容器内元素并遍历元素的数据类型

迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有机的统一起来。

eg:

    string str("basketball nba");

if(str.begin()!=str.end())

{

    auto i=str.begin();

    *i=toupper(*i);

}

注意迭代器失效

但凡是使用了迭代器的循环体,都不要向迭代器所属的容器添加元素

 

数组

数组是一种复合类型,类似于vector的数据结构,但在性能和灵活性的权衡上又与vector有所不同。

数组不能随意添加元素

数组大小固定

如果不清楚元素确切的个数,使用vector

 

数组不允许拷贝赋值

eg:

int a[]={1,2,3};

int a2[]=a;    //error,不允许使用一个数组初始化另一个数组

a2=a;            //error,不能把一个数组直接赋值给另一个数组

 

复杂的数组声明

eg:

int *ptrs[10];                           //ptrs是含有10个整型指针的数组

int &refs[10]=/*?*/;              //error,数组不存在引用

int (*Parray)[10]=&arr;        //Parray指向一个含有10个整数的数组

int (&arrRef)[10]=arr;        //arrRef引用一个含有10个整数的数组

 

指针数组与数组指针的区别

指针数组 字符数组 整数数组 --- 前两个个字指的是数组元素类型

数组指针 字符指针 整数指针 --- 前两个个字指的是指针的类型

1.指针数组是一个数组,只不过它存放的元素都是指针 例如:int *a[10];

2.数组指针是一个指针,指向数组类型的指针  例如:int (*a)[10];

 

指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身的大小决定,每一个元素都是一个指针,在32 位系统下任何类型的指针永远是占4 个字节。它是“储存指针的数组”的简称。
数组指针:首先它是一个指针,它指向一个数组。在32 位系统下任何类型的指针永远是占4 个字节,至于它指向的数组占多少字节,不知道,具体要看数组大小。它是“指向数组的指针”的简称。

 

 

eg:

#include <iostream>

using namespace std;

void funcintarray(int *a)

{

    for (int i = 0; i < 10; i++)

        cin >> *(a + i);

    for (int i = 0; i < 10; i++)

        cout << *(a + i)+1 << " ";

    cout << endl;

    for (int i = 0; i < 10; i++)

        cout << &a + i << " ";

    cout << endl;

    cout << a+1 << endl;

    cout << a + 2 << endl;

}

int main()

{

    int a[10];

    funcintarray(a);

    system("pause");

    return 0;

}

 

 

#include <iostream>

using namespace std;

void funcDoubleIntArray(int (*b)[5])

{

    for (int i = 0; i < 2; i++)

        for (int j = 0; j < 5; j++)

            cin >> *(*(b + i) + j);

    for (int i = 0; i < 2; i++)

        for (int j = 0; j < 5; j++)

            cout << *(*(b + i) + j) << " ";

    cout << endl;

}

int main()

{

    int a[2][5] = {{ 1,2,3,4,5 },{ 6, 7, 8, 9, 10 }};

    int(*b)[5];

    b = a;

    for (int i = 0; i < 2; i++)

        for (int j = 0; j < 5; j++)

            cout << *(*(b + i) + j) << " ";

    cout << endl;

    funcDoubleIntArray(b);

    system("pause");

    return 0;

}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值