c++ 字符串

字符串 

C++ 提供了两种类型的字符串表示形式:

  • C 风格字符串
  • C++ 引入的 string 类类型

 一、C风格字符串

字符串实际上是使用 null 字符 '' 终止的一维字符数组。因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

下面的声明和初始化创建了一个 "Hello" 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 "Hello" 的字符数多一个。

char greeting[6] = {'H', 'e', 'l', 'l', 'o', ''};

 

依据数组初始化规则,您可以把上面的语句写成以下语句:

char greeting[] = "Hello";

字符串的内存表示:
c++ 字符串

不需要把 null 字符放在字符串常量的末尾。C++ 编译器会在初始化数组时,自动把 '' 放在字符串的末尾

#include <iostream>
 
using namespace std;
 
int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', ''};
 
   cout << "Greeting message: ";
   cout << greeting << endl;
 
   return 0;
}

 

Greeting message:Hello

 

二、字符串函数

c++ 字符串

 

 实例:

#include <iostream>
#include <cstring>
 
using namespace std;
 
int main ()
{
   char str1[11] = "Hello";
   char str2[11] = "World";
   char str3[11];
   int  len ;
 
   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
 
   return 0;
}

 

 运行结果:

strcpy( str3, str1) : Hello

strcat( str1, str2): HelloWorld

strlen(str1) : 10

 

 

 

三、C++ 中的 String 类 ———  C++ 标准库提供string 类类型

 实例:

#include <iostream>
#include <string>
 
using namespace std;
 
int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;
 
   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;
 
   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;
 
   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;
 
   return 0;
}

 

 运行结果:

str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10

 

 

 

 

补充:

1. string类提供了一系列针对字符串的操作,比如:

  • 1. append() -- 在字符串的末尾添加字符
  • 2. find() -- 在字符串中查找字符串
  • 4. insert() -- 插入字符
  • 5. length() -- 返回字符串的长度
  • 6. replace() -- 替换字符串
  • 7. substr() -- 返回某个子字符串
#include <iostream>
#include <string>
using namespace std;

int main()
{
    //定义一个string类对象
    string http = "www.runoob.com";

   //打印字符串长度
   cout<<http.length()<<endl;

    //拼接
    http.append("/C++");
    cout<<http<<endl; //打印结果为:www.runoob.com/C++

    //删除
    int pos = http.find("/C++"); //查找"C++"在字符串中的位置
    cout<<pos<<endl;
    http.replace(pos, 4, "");   //从位置pos开始,之后的4个字符替换为空,即删除
    cout<<http<<endl;

    //找子串runoob
    int first = http.find_first_of("."); //从头开始寻找字符'.'的位置
    int last = http.find_last_of(".");   //从尾开始寻找字符'.'的位置
    cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并打印

    return 0;
}

 

 

 2. cin.getline() 是在输入一段字符完成后开始读取数据(注意,是输入完成后,以Enter为结束标志)

 实例:输入一串字符,编程统计其中的数字个数和英文字母个数。输入的字符以 # 为结束标志。

#include<iostream>
using namespace std;

#define N 100
int main()
{
    char X[N];
    cin.getline(X,N);                               //以cin.getline形式输入
    int a=0,b=0;
    for(int i=0;i<N;i++)
    {
        if(X[i]=='#')                                      //为#为结束标志
            break;
        if(X[i]>='0'&&X[i]<='9')
            a++;                                         //统计数字个数
        if((X[i]>='a'&&X[i]<='z')||(X[i]>='A'&&X[i]<='Z'))
            b++;                                      //统计英文字母个数
    }
    cout<<a<<endl<<b<<endl;
    return 0;
}

 

 

 

 3.字符串与vector

c++ 字符串

 

 

4.sizeof 和 strlen

c++ 字符串

c++ 字符串

 

 

 5.关于字符数组为什么可以以数组名来用cout输出数组内容,而普通数组不行

 c++ 字符串

 

 

 6.<cstring> 创建详解

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{

    //1. 字符串的创建
    cout<<"第一:字符串的创建!nn";
    string a(4,'a');
    cout<<"1.以 a 为原字符 4单位大小nn";
    cout<<"string a(4,'a');ncout<<a<<endl;n";
    cout<<a<<endl<<endl;

    cout<<"2.任意大小的字符串nn";
    cout<<"string b("bbbbbb");ncout<<b<<endl;n";
    string b("bbbbbb");
    cout<<b<<endl<<endl;

    cout<<"3.把某一字符串的某一部分n(0位置开始4个长度)给cnn";
    cout<<"string c(a,0,4) ;ncout<<c<<endl;n";
    string c(a,0,4) ;
    cout<<c<<endl<<endl;

    cout<<"4. 10长度原长度不足补*;nn";
    cout<<"c.resize(10,'*');ncout<<c<<endl;n";
    c.resize(10,'*');
    cout<<c<<endl<<endl;

    system("pause");
    system("cls");
    return 0;
}

 

 

 

 

7.<cstring> assign() 、 copy() 详解

 

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{

    cout<<"第二: 字符串的赋值 assign();"<<endl;
    cout<<"1.感觉就像是append不过是抹除-覆盖n";
    cout<<"string e;nchar f[10]="123456"ne.assign(f);ne+=' ';ncout<<e<<endl<<endl;n";
    string e;
    char f[10]="123456";
    e.assign(f);
    e+=' ';
    cout<<e<<endl<<endl;

    cout<<"2.string区间 赋值都类似吧n";
    cout<<"e.assign(f,3,3);ne+=' ';ncout<<e<<endl<<endl;ne.assign(f,3);ncout<<e<<endl;n";
    e.assign(f,3,3);
    e+=' ';
    cout<<e<<endl;
    e.assign(f,3);
    cout<<e<<endl<<endl;

    cout<<"3.某字符串char型 全部n";
    cout<<"char ssr[10]="asdqwezxc";ne.assign(ssr);ncout<<ssr<<endl;n";
    char ssr[10]="asdqwezxc";
    e.assign(ssr);
    cout<<ssr<<endl<<endl;

    cout<<"4.某字符串char型 前num个n";
    cout<<"e.assign(ssr,4);ncout<<e<<endl;n";
    e.assign(ssr,4);
    cout<<e<<endl<<endl;

    cout<<"5.某字符赋值n";
    cout<<"赋值3个6n";
    e.assign(3,'6');
    cout<<e<<endl<<endl;

    cout<<"copy() 将d中的2位置开始的12个字符覆盖到char型数组ss上n 必须为-> char型 <-否则报错";
    cout<<" char ss[10]="123";n string dd;nd.copy(ss,12,2);ncout<<ss<<endl;n";
    char ss[15]="123";
    string dd("abcdefghijklmn");
    dd.copy(ss,12,2);
    cout<<ss<<endl<<endl;

    system("pause");
    system("cls");
        return 0;
}

 

 

 

 8.<cstring> append() 详解及其扩展(int, char):

 

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{   cout<<"第三: 字符串的添加与复制 append();nstring d(a);ncout<<d<<endl;n//或者 d=d+a;/nd.append(b);n";

    cout<<"1.在d的末尾添加字符串ann";
    string d(a);
    d.append(b);
    cout<<d<<endl<<endl;

    cout<<"2.在d的末尾添加字符串/nb(0位置开始,2个长度)的数据nn";
    cout<<"d.append(b,0,2);ncout<<d<<endl;n";
    d.append(b,0,2);
    cout<<d<<endl<<endl;

    cout<<"3.添加4个 ~ 字符nn";
    cout<<"d.append(4,'~');n";
    cout<<"cout<<d<<endl<<endl;n";
    d.append(4,'~') ;
    cout<<d<<endl<<endl;

    system("pause");
    system("cls");

    cout<<"4. int 与 char 型添加 (好高兴自己想到的int ^_^ )n";
    cout<<"char app[100]="aaabbb";";
    cout<<"nstring charr("-_-");n";
    cout<<"charr.append(app);ncout<<charr<<endln";
    char app[100]="aaabbb";
    string charr("-_- ");
    charr.append(app);
    cout<<charr<<endl<<endl;

    cout<<"charr.append(app,4);ncout<<charr<<endl<<endl;n";
    charr.append(app,4);
    cout<<charr<<endl<<endl;

    cout<<"char型数组全部,char型数组的前4个nn****如果要添加中间***n";
    cout<<"string tmp;nstring tmp;tmp.assign(app);ncharr.assign("");ncharr.append(tmp,1,4);ncout<<charr<<endl<<endl;n";
    string tmp;
    tmp.assign(app);
    charr.assign("");
    charr.append(tmp,1,4);
    cout<<charr<<endl<<endl;

    cout<<"5.int  double 等等 通过 sprintf() <cstdio>作为转接n";
    cout<<"int aaa=15314;ndouble bbb=3.1415;nchar aa[10];nsprintf(aa,"%d",aaa);ncharr.append(aa,0,4);nsprintf(aa,"%f",bbb);ncharr.append(aa,0,4);ncout<<charr<<endl;n";
    int aaa=15314;
    double bbb=3.1415;
    char aa[10];
    sprintf(aa,"%d",aaa);
    charr.append(aa,0,4);
    sprintf(aa,"%f",bbb);
    charr.append(aa,0,4);
    cout<<charr<<endl<<endl;

    system("pause");
    system("cls");
    return 0;
}

 

原文链接: https://www.cnblogs.com/expedition/p/11343600.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ 字符串

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/300822

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午9:45
下一篇 2023年2月15日 下午9:45

相关推荐