曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 2673|回复: 0
打印 上一主题 下一主题

使用字符串

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34387
跳转到指定楼层
楼主
发表于 2011-6-21 00:03:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在 C++ 里使用字符串,必须先把 string 头文件包括到程序里,该文件定义了必要的功能。C++ 字符串变量的声明和 C 不太一样,它的声明形式为:
std::string strval;
strval = "hello world";

还可以用字符串连接操作符 '+' 将字符串变量所代表的字符串连接起来。

测试代码
[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    std::string str1, str2, str3, str4;

    str1 = "hello";
    str2 = "cpp";
    str3 = "world";

    str4 = str1 + " " + str2 + " " + str3;

    cout << "Press enter to continue" << endl;
    cin.get();

    cout << str4 << endl;

    return 0;
}

运行输出:
$ ./strtest
Press enter to continue

hello cpp world
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-21 13:08 , Processed in 0.098007 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表