C++ 语法
  DLjUyo7XFbHQ 2023年11月30日 20 0

C++ 语法

#include <iostream>
using namespace std;
int main() {
    //控制台输出
    std::cout << "Hello, World!" << std::endl;

    int num = 10; // 定义一个整型变量num,并赋值为10

    if (num > 5) {
        cout << "num>5" << endl;
    } else {
        cout << "num小于等于5" << endl;
    }
    //for
    for (int i = 0; i < 5; i++) {
        std::cout << "cout:" << i << std::endl;
    }

    // 创建一个字符串
    std::string str = "Hello, world!";

    // 获取字符串长度
    std::cout << "str length:" << str.length() << std::endl;

    // 字符串连接
    std::string anotherStr = " How are you?";
    std::string combinedStr = str + anotherStr;
    std::cout << "combinedStr:" << combinedStr << std::endl;

    // 从字符串中获取子串
    std::string subStr = str.substr(7, 5); // 从位置7开始,长度为5的子串
    std::cout << "str:" << subStr << std::endl;

    // 查找子串
    size_t found = str.find("world");
    if (found != std::string::npos) {
        std::cout << "'world',index:" << found << std::endl;
    } else {
        std::cout << "err 'world'" << std::endl;
    }
    return 0;
}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月30日 0

暂无评论

推荐阅读
  8Tw5Riv1mGFK   2024年05月01日   82   0   0 C++
  BYaHC1OPAeY4   2024年05月08日   58   0   0 C++
  yZdUbUDB8h5t   2024年05月05日   44   0   0 C++
  oXKBKZoQY2lx   2024年05月17日   62   0   0 C++
DLjUyo7XFbHQ