C++ string的使用及函数(笔记)

c++中string的使用

没什么废话想说
进入正题

string头文件
当然要使用string必须添加头文件 是吧!!

#include <string>

string的创建和初始化
string支持多种初始化方式
比如:

    //1.
    string a;
    //最普通的创建方式
    //2.
    string b(5, 'a');
    //b中初始化为含有5个‘a’的串
    //3.
    string c(b);
    //将b的元素逐个拷贝到c中
    //4.
    string d(c.begin(), c.begin() + c.size() / 2);
    //把c中前一半元素拷贝到d
    //5.
    char arry[] = "world!!!!!";
    string e(arry, 5);
    //将arry字符串前五个元素赋值给e
    cout << " e : "<< e << endl;
    cout << endl;
    //6.
    string f(arry + 5, 4);
    //将arry字符串下标为5开始的四个元素逐个拷贝到f
    cout << " f : " << f << endl;
    cout << endl;
    //7.
    char null[] = {'h', 'i', 'j'};
    //字符数组也可以
    string g(null, 2);
    cout << " g : " << g << endl;
    cout << endl;
    //8.
    a = "hiveds";
    string h(a, 2);
    //将a字符串下标为2开始到最后的所有元素赋值给h
    cout << " h : " << h << endl;
    cout << endl;
    string i(a, 0, 4);
    //将a字符串下标为0开始的四个元素拷贝到i中
    cout << " i : " << i << endl;

string的find函数的各种操作形式

  1. find();
  2. find_first_of();
  3. find_first_not_of();
  4. find_last_of();
  5. find_last_not_of();
    一共这五种查找函数,相同点是查找到会返回查找元素所在下标,若未找到返回string::nops
    ps(string::nops可以理解为string类中的NULL)

具体操作和用法见代码:

#include <bits/stdc++.h>
using namespace std;
//string的查找函数
int main()
{
    string s("abcdefghifk"); //初始化一个string串
    string::size_type pos = s.find("bc");
    //找到返回第一个字符出现的位置
    //未找到返回string::npos
    //find(从前往后找)
    //rfind(从后往前找)返回结果都是下标
    if(pos==string::npos)  //未找到返回string::npos
        cout << "no find";
    else
        cout << "find in: " << pos << endl;

    s = "r2%%d20/e5s6";
    string num("0123456789");
    pos = s.find_first_of(num);
    //在s串中从前往后找num串存在的任意一个字符第一个出现的位置
    if(pos==string::npos)
        cout << "no find";
    else
        cout << "it find in num: " << pos << endl;

    //若要寻找串中所有的元素 利用循环 采用两种函数
    //从前向后查找出现的全部数字
    //find_first_of
    pos = 0;
    cout << "find first of in num :" << endl;
    while((pos=s.find_first_of(num,pos))!=string::npos)
    {
        cout << s[pos] << " ";
        pos++;
    }
    cout << endl;
    //从后向前查找
    //s.find_last_of()
    pos = 0;
    cout << "find last of in num :" << endl;
    while((pos=s.find_last_of(num,pos))!=string::npos)
    {
        cout << s[pos] << " ";
        pos++;
    }
    cout << endl;
    
    //寻找出现的全部字母
    string letters("abcdefghigklmnopqrstuvwxyz");
    pos = 0;
    cout << "find first of int letters :" << endl;
    while((pos=s.find_first_of(letters,pos))!=string::npos)
    {
        cout << s[pos] << " ";
        pos++;
    }
    cout << endl;

    //查找出现的全部非数字元素
    pos = 0;
    cout << "find first not of in num :" << endl;
    while((pos=s.find_first_not_of(letters,pos))!=string::npos)
    {
        cout << s[pos] << " ";
        pos++;
    }
    cout << endl;
    return 0;
}

string的插入和赋值

  1. insert(); 插入
  2. assign(); 赋值
    这两函数也有多种使用方式
#include <bits/stdc++.h>
using namespace std;
//修改string对象的方法
//insert插入
//assign赋值
int main()
{
    string s1("hello");
    string s2("abcdef");
    //初始化两个字符串
    string ::iterator p = s1.begin();
    //让迭代器p指向s1的第一个元素
    s1.insert(p, 'A');
    //在迭代器指向的位置前插入字符A 
    cout << s1 << endl << endl;
    
    p++;
    s1.insert(p, 3, 'B');
    //在迭代器指向的位置前插入三个字符B
    cout << s1 << endl << endl;

    s2.insert(s2.begin(), s1.begin(), s1.end());
    //将s串全部拷贝到s2串之前
    cout << s2 << endl  << endl;
    
    s2.insert(0, 3, 'H');
    //在s2的下标0前插入3个H
    cout << s2 << endl  << endl;
    
    s2.assign(s1.begin() + 1, s1.end());
    //将s2元素清空 并把迭代器指向的区间全部赋值给s1(左闭右开)
    cout << s2 << endl << endl;
    
    s2.assign(8, 'K');
    //将s2中元素清空 赋值成8个k
    cout << s2 << endl << endl;
    
    s1 = "abcdef";
    s1.erase(s1.begin());
    //通过迭代器删除了s1的第一个元素
    cout << s1 << endl << endl;
    return 0;
}

string的比较方式
string的比较是ascll码值的比较
区分大小写 小写字母均大于大写字母
比较方式可以是运算符比较 也可以采用函数比较
代码如下:

#include <bits/stdc++.h>
using namespace std;
//string类型的比较
//区分大小写 小写字母均大于大写字母 (asccl码比较)
int main()
{
    string s1("abcxyz");
    string s2("abdxyz");
    //运算符比较
    //逐个字符比较 遇到不同的字符就比较二者ascll码大小 第一个不等字符的ascll码大的即为大
    if(s1!=s2)
        cout << "no same" << endl;
    if(s1==s2)
        cout << "same" << endl;
    if(s1>s2)
        cout << "s1 biger" << endl;
    else
        cout << "s2 biger" << endl;

    //compare函数比较 规则相同
    //逐个字符比较 遇到不同的字符就比较二者ascll码大小 第一个不等字符的ascll码大的即为大
    cout << "compare:" << endl;
    if(s1.compare(s2)>0)
        cout << "s1 biger" << endl;
    if(s1.compare(s2)<0)
        cout << "s2 biger" << endl;
    if(s1.compare(s2)==0)
        cout << "same" << endl;
      
   
    if(s1.compare(3,3,s2)>0)
    //将s1串下标为3开始的三个字符与s2比较
        cout << "compare(3,3,s2)" << "s1 biger" << endl;
    if(s1.compare(3,3,s2,3,3)==0)
    //将两串下标为3开始的三个字符比较
        cout << "compare(3,3,s2,3,3) "<< "s1=s2" << endl;

    //也可以与c的字符指针比较
    char *cp = "aassdd";
    if(s1.compare(3,3,cp)>0)
        cout << "s1(3,3) > cp" << endl;
    if(s1.compare(3,3,cp,3,3)>0)
        cout << "s1(3,3) > cp(3,3) ";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lijing_er/article/details/104645736