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