[C++] C++ code framework① (Create C++ project in Visual Studio 2019 | iostream standard stream | std standard namespace | cout console output)





1. Create a C++ project in Visual Studio 2019



Open Visual Studio 2019, select the "menu bar/file/new/project" option, and create a new project;

insert image description here

Select the console program of the C++ language on the Windows platform ;

Choose to create "Empty Project" and click the "Next" button in the lower right corner;

insert image description here

Enter the project name and location to create the project;
insert image description here

In the "Solution Explorer", right-click on "Source Files", select "Add/New Item";

insert image description here

In the "Add New Item" window, choose to add the "C++ File (.cpp)" file, and set the name to "hello_world.cpp";

Then, click the "Add" button in the lower right corner to add the C++ source code file;

insert image description here

In the "Solution Explorer", the created source code is displayed under the source file directory;

insert image description here





Two, C++ code writing



All C++ programs must first include the header file of the standard IO stream and use the std standard namespace;


1. iostream standard stream


Use #include "iostream"the standard IO stream headers that include C++;

// 包含 C++ 头文件
#include "iostream"

iostream standard IO stream for processing

  • standard input and output
  • file input and output

etc. operation;


iostream commonly used standard input and output streams:

  • cin: standard input stream , read data from the standard input device;
  • cout : standard output stream , output data to standard output device;
  • cerr: standard error stream , output error information to the standard error device;
  • fstream : standard file input and output stream , read data from the file, output data to the file;

After including the iostream header file, the above input and output streams can be used;


2. std standard namespace


Use stdthe standard namespace, in which many standard definitions are defined;

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

using namespace std;The use standard namespace code in the above code is a preprocessing directive, which is used to inform the compiler to use stdthe standard namespace in the current source code;

stdThe namespace is an abbreviation of the English word "standard", which defines all elements of the standard library, such as: cout, cin, string, etc.;

  • If you do not use stdthe standard namespace, you must add a prefix when using the elements , such as: , , etc.;std::std::coutstd::cinstd::string
  • If the standard namespace is used , elements such as cout , cin , and string can be used directly without adding a prefix;stdstd::

3. cout console output


In C language, use the printf function to output data to the console, and this method can still be used in C++;

	// 使用 C 语言的方式在控制台输出文本
	printf("printf Hello World\n");

It is more convenient to use cout standard stream output in iostream in C++;

coutStandard output stream, which can output data to standard output (console) , where its role is to perform standard output and output content to the console;

The left shift operator << operator is used to send the string content data on the right to the stream on the left, that is, to "cout Hello World"send the string data to coutthe standard output stream;

endlThe role of the operator is to refresh the output stream, print the content to the console and enter a new line;

	// 使用 C++ 的方式在控制台输出文本
	//		cout 的作用是进行标准输出 , 向控制台输出内容
	//		C++ 中的 左移操作符 << 
	//			在 C++ 语言中进行了操作符重载 进行了功能增强
	//		endl 的作用是 将内容打印到控制台 并且回车换行
	cout << "cout Hello World" << endl;

4. Code example


Code example:


// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

int main() 
{
    
    
	// 使用 C 语言的方式在控制台输出文本
	printf("printf Hello World\n");

	// 使用 C++ 的方式在控制台输出文本
	//		cout 的作用是进行标准输出 , 向控制台输出内容
	//		C++ 中的 左移操作符 << 
	//			在 C++ 语言中进行了操作符重载 进行了功能增强
	//		endl 的作用是 将内容打印到控制台 并且回车换行
	cout << "cout Hello World" << endl;
	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
}

Results of the :

printf Hello World
cout Hello World
Press any key to continue . . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132297247
Recommended