c++中的string变量


目录
  • 一 string变量的定义和初始化
  • 二 string变量的输入输出
  • 三 读取一行getline(),判空empty(),大小长度size()、length()
  • 四 string字符串的比较
  • 五 string字符串的加法

一 string变量的定义和初始化

#include 
#include 
#include 

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;
}

运行结果:

二 string变量的输入输出

#include 
#include 
#include 

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;
}

运行结果:

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

#include 
#include 
#include 

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;
}

运行结果:


四 string字符串的比较

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

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;
}

运行结果:

五 string字符串的加法

#include 
#include 
#include 

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;
}

运行结果: