c++中的string变量

一 string变量的定义和初始化

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    /* string变量的定义和初始化 */

    // 1.定义一个字符串girlFriend1,把字符串常量"王丽"拷贝到girlFriend1
    string girlFriend1;
    girlFriend1 = "王丽";
    cout << "girlFriend1 = " << girlFriend1 << endl;

    // 2.定义一个字符串girlFriend2,把字符串变量girlFriend1的值拷贝到girlFriend2
    string girlFriend2;
    girlFriend2 = girlFriend1;
    cout << "girlFriend2 = " << girlFriend2 << endl;

    // 3.定义一个字符串girlFriend3,同时使用字符串常量"李娜"来初始化
    string girlFriend3("李娜");
    cout << "girlFriend3 = " << girlFriend3 << endl;

    // 4.定义一个字符串girlFriend4,使用字符串变量girlFriend3来初始化
    string girlFriend4(girlFriend3);
    cout << "girlFriend4 = " << girlFriend4 << endl;

    string girlFriend5(10, 'A');  // 相当于string girlFriend5("AAAAAAAAAA");
    cout << "girlFriend5 = " << girlFriend5 << endl;

    return 0;
}

运行结果:
image

二 string变量的输入输出

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    /* string变量的输入输出 */

    // 1.基本的输入输出
    string job;
    cout << "你是做什么工作的呢?" << endl;
    cin >> job;
    cout << "做" << job << "收入一定不错吧" << endl;
    cout << endl;

    // 2.自动跳过空白字符
    string university;  //大学
    string profession;  //专业
    cout << "你是哪个学校毕业的?学什么专业?" << endl;
    cin >> university >> profession;
    cout << university << "的" << profession << "专业不错哦" << endl;
    cout << endl;

    // 3.连续输入多个字符串,个数不确定
    string food;
    int i = 0;
    cout << "请输入你喜欢的食物:";
    while (cin >> food)  // 当用户输入文件结束符(Ctrl+z)并回车 cin >> food 返回0
    {
        i++;
        cout << "你喜欢的第" << i << "种食物是:" << food << endl;
        cout << "你还喜欢什么美食:";
    } // 控制台输入Ctrl+z可以结束循环
    cout << "你喜欢的食物有" << i << "种" << endl;

    system("pause");
    return 0;
}

运行结果:
image

三 读取一行getline(),判空empty(),大小长度size()、length()

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    string address;

    cout << "美女,你准备去哪里旅游呀?";

    // getline() 从标准输入设备读取一行,直到遇到回车符,但是不包括最后输入的回车符
    getline(cin, address);

    // empty() 可以判断字符串是否为空,为空返回true,不为空返回false
    if (address.empty())
    {
        cout << "你输入了一个空串!" << endl;
    } 
    else
    {
        cout << "太巧了,我也准备去" << address << "旅游" << endl;
    }

    // size() 和 length() 可以计算字符串的长度
    cout << "size方法计算的 地址的长度是:" << address.size() << endl;
    cout << "length方法计算的 地址的长度是:" << address.length() << endl;

    system("pause");
    return 0;
}

运行结果:
image
image
image

四 string字符串的比较

  • 字符串比较是按字符的ASCII码进行对比
  • 字符串比较结果bool类型,true逻辑真,false逻辑假
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    string myGirl = "王丽";
    string yourGirl;

    cout << "我的女朋友是" << myGirl << ",你的女朋友是谁呢?" << endl;
    cin >> yourGirl;

    if (myGirl == yourGirl)
    {
        cout << "太巧了,我们都喜欢" << myGirl << ",我们决斗吧!" << endl;
    } 
    else
    {
        cout << "祝你幸福" << endl;
    }

    system("pause");
    return 0;
}

运行结果:
image
image

五 string字符串的加法

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    string s1 = "张三";
    string s2 = "喜欢";
    string s3 = "c++";

    s1 = s1 + s2;
    cout << s1 << endl;
    s1 += s3;
    cout << s1 << endl;

    system("pause");
    return 0;
}

运行结果:
image

六 成员函数

成员函数 说明
append 向字符串的末尾添加字符。
assign 对字符串的内容赋新的字符值。
at 返回对字符串中指定位置的元素的引用。
back 对字符串中最后一个元素的引用,必须为非空值
begin 返回发现字符串中第一个元素的位置的迭代器。
c_str 将字符串的内容转换为以 null 结尾的 C 样式字符串。
capacity 返回在不增加字符串内存分配的情况下可存储在字符串中的元素的最大数目。
cbegin 返回发现字符串中第一个元素的位置的常量迭代器。
cend 返回发现字符串中最后一个元素之后的位置的常量迭代器。
clear 清除字符串中的全部元素。
compare 将字符串与指定字符串比较,确定两个字符串是否相等或按字典顺序一个字符串是否小于另一个。
copy 将指定数目的字符从源字符串中的索引位置复制到目标字符组。 已弃用。 请改用 basic_string::_Copy_s。
crbegin 返回发现反向字符串中第一个元素的位置的常量迭代器。
crend 返回发现反向字符串中最后一个元素之后的位置的常量迭代器。
_Copy_s 将指定数目的字符从源字符串中的索引位置复制到目标字符组。
data 将字符串的内容转换为字符数组。
empty 测试字符串是否包含字符。
end 返回发现字符串中最后一个元素之后的位置的迭代器。
ends_withC++20 检查字符串是否以指定的后缀结尾。
erase 从字符串中的指定位置删除一个或一系列元素。
find 向前搜索字符串,搜索与指定字符序列匹配的第一个子字符串。
find_first_not_of 在字符串中搜索不是指定字符串的任何元素的第一个字符。
find_first_of 在字符串中搜索与指定字符串中任何元素匹配的第一个字符。
find_last_not_of 在字符串中搜索不是指定字符串的任何元素的最后一个字符。
find_last_of 在字符串中搜索属于指定字符串中一个元素的最后一个字符。
front 返回对字符串中第一个元素的引用。
get_allocator 返回用于构造字符串的 allocator 对象的副本。
insert 将元素、多个元素或一系列元素插入到指定位置的字符串中。
length 返回字符串中元素的当前数目。
max_size 返回字符串可包含的字符的最大数目。
pop_back 删除字符串的最后一个元素。
push_back 在字符串的末尾处添加一个元素。
rbegin 返回指向反向字符串中第一个元素的迭代器。
rend 返回指向刚超出反向字符串的最后一个元素的位置的迭代器。
replace 用指定字符或者从其他范围、字符串或 C 字符串复制的字符来替代字符串中指定位置的元素。
reserve 将字符串的容量设置为一个数目,这个数目至少应与指定数目一样大。
resize 根据要求追加或删除元素,为字符串指定新的大小。
rfind 向后搜索字符串,搜索与指定字符序列匹配的第一个子字符串。
shrink_to_fit 放弃字符串的超出容量。
size 返回字符串中元素的当前数目。
starts_withC++20 检查字符串是否以指定的前缀开头。
substr 从字符串起始处的指定位置复制最多某个数目的字符的子字符串。
swap 交换两个字符串的内容。
posted @ 2022-04-03 10:06  荒年、  阅读(517)  评论(0编辑  收藏  举报