目录
入口函数
常用的预编译语句
Hello World的编译运行
标准输入输出
行注释与段落注释
命名空间
While循环
for循环
if条件
入口函数
就像多数编程语言一样,入口函数是程序首要执行的位置,c++也不例外,它的入口函数也是一个main函数
int main()
{
return 0;
}
那么对于这个入口函数的返回值,一般用于表示程序是否正常运行,0表示正常,非0表示异常。
int main(int argc, char* argv[])
{
return 0;
}
使用两个入参的main方法也是c++中常见的入口函数,其中argc表示参数的个数,argv表示实际的入参,往往argv[0]表示程序的名称,但是不要过于依赖这个值,它也可能是空值。调用的时候实际使用的入参从argv[1]开始。
常用的预编译语句
| 预编译语句 | 含义 |
| #include [file] | 表示引用某个文件 |
| #ifdef [id] #endif #ifndef [id] #endif | 条件预编译 |
| #define [id] [value] | 类型替换符或常量声明 |
| #pragma [xyz] | 独立编译的标记, 例如,#pragma once表示只编译一次 |
Hello World的编译运行
1. 在任意目录下创建Hello.cpp文件
touch Hello.cpp
2. 编写Hello World
#includeusing namespace std; int main() { cout << "Hello World" << endl; return 0; }
在c++20以上,可以使用import代替#include
import; using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
3. 编译代码
使用g++指令进行编译,默认生成a.out文件
-o参数用于指定编译后的文件名称
4. 执行代码
使用./a.out或者./Hello可直接执行编译后的代码
标准输入输出
标准输入即从窗口读入数据到程序,标准输出即从程序输出数据到窗口。包括:
cin: 读作see-in, 标准输入
cout: 读作see-out, 标准输出
cerr: 读作see-err, 输出警告或错误
clog: 读作see-log,输出基础日志信息
使用示例,从窗口读入两个数字,计算他们的和:
#includeint main() { std::cout << "Enter two numbers:" << std::endl; int v1 = 0, v2 = 0; std::cin >> v1 >> v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0; }
#include就是对“包”的引用,但是c++没有包的概念,它是头文件的概念,是对标准库中的头文件引用,类似于JAVA的import。
这里我们看到了std::这是一种全限定使用的方式,类似于JAVA中的
java.util.concurrent.ConcurrentHashMap一样,属于全限定使用,指定包名的方式使用类或方法,这样比较繁琐,我们可以进行改造:
#includeusing namespace std; int main() { cout << "Enter two numbers:" << endl; int v1 = 0, v2 = 0; cin >> v1 >> v2; cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl; return 0; }
在include下方使用using namespace std的声明,这样相当于把std进行了静态引用,可以直接使用类和方法了,减少了后续的繁琐。
首先,通过cout的方法输出信息到窗口,endl表示换行,并强制flush数据到窗口。
cout << "Enter two numbers:" << endl;
紧接着,定义了两个变量,cin用于接收窗口输入,存储数据
int v1 = 0, v2 = 0; cin >> v1 >> v2;
最后打印结果
cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl;
在c++20版本中引入了cout::format方法,可以对占位符进行替换
std::cout << std::format("There are {} ways I love you.", 219) << std::endl;
行注释与段落注释
#include
int main()
{
// prompt user to enter two numbers
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0; // variables to hold the input we read
std::cin >> v1 >> v2; // read input
std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;
return 0;
}
可以使用//进行行注释,可单独处于一行,也可以在代码后方。
也可以使用进行段落注释。
行注释和段落注释都属于注释,不会被实际执行。
命名空间
命名空间为了解决不同代码中声明了相同的方法或变量,导致的冲突问题。假如你的代码中声明了foo方法,第三方库中也声明了foo方法,为了解决冲突,可以给自己的代码加上namespace:
namespace mycode {
void foo()
{
std::cout << "foo() called in the mycode namespace" << std::endl;
}
}
这样,我们在使用的时候可以指定命名空间进行引用了
mycode::foo();
上面的引用方式适合出现冲突的时候,当namespace没有发生冲突时,这样根据命名空间引用方法的方式让代码变得冗余,可以通过import简化:
using namespace mycode;
int main()
{
foo(); // Implies mycode::foo();
}
当然,命名空间的引用也可以采用精确引用的方式:
using std::cout; cout << "Hello, World!" << std::endl;
这里的using std::cout表示仅引入cout,其他仍需使用命名空间的方式调用,如std::endl
当然,命名空间也是可以嵌套的,这里是三层嵌套的示例:
namespace MyLibraries {
namespace Networking {
namespace FTP {
}
}
}
在c++17以后,也可以进行简化:
namespace MyLibraries::Networking::FTP {
}
在引用时,我们可以给一个别名:
namespace MyFTP = MyLibraries::Networking::FTP;
While循环
#include
int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}
while的逻辑是:只要满足条件,我就不停。其基本格式:
while (condition)
statement
for循环
#include
int main()
{
int sum = 0;
// sum values from 1 through 10 inclusive
for (int val = 1; val <= 10; ++val){
sum += val; // equivalent to sum = sum + val
}
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}
for循环的逻辑是:已知范围内的数据遍历。
if条件
if (val == currVal) {
// if the values are the same
++cnt; // add 1 to cnt
} else {
// otherwise, print the count for the previous value
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
currVal = val; // remember the new value
cnt = 1; // reset the counter
}
满足条件则执行A方案,不满足条件则执行B方案。



