C++ Algorithm Tutorial (1)

2024-03-02 00:44
文章标签 c++ tutorial algorithm

本文主要是介绍C++ Algorithm Tutorial (1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

中文版 c++算法入门教程(1)_c++怎么学习算法-CSDN博客

C++is a powerful and widely used programming language, and for those who want to delve deeper into programming and algorithms, mastering C++is an important milestone. This article will take you step by step to understand the basic knowledge of C++programming, and introduce some common algorithms and programming skills to help you get started with C++algorithms.

1. What is Programming?

Programming is the process of using a specific programming language to write a series of instructions or code to tell a computer to perform specific tasks or solve problems. It aims to translate abstract ideas and concepts into instructions that a computer can understand and execute. Programming is used to develop various software applications, websites, games, etc., and allows people to leverage the power of computers for automation, data processing, and innovation.

Here are some important milestones in the development of programming. Interested parties can take a look:

Mechanical computing equipment: In the late 19th and early 20th centuries, a series of mechanical computing equipment emerged, such as differential machines and analytical machines. These mechanical devices require physical settings to perform computational tasks, rather than programming.

Relay and electronic tube: In the mid-20th century, the invention of relays and electronic tubes made computers more reliable and efficient. These devices are capable of performing logical and arithmetic operations, but programming is still done in physical ways (such as patch panels, punched paper tapes).

Binary systems and assembly languages: The popularity of binary systems has enabled computers to represent and process information in digital form. With the introduction of binary systems, assembly language has also emerged. Assembly language uses mnemonics and symbols to represent machine instructions, simplifying the programming process.

Advanced programming languages: In the late 1950s and early 1960s, advanced programming languages began to emerge. These languages (such as Fortran, COBOL, and ALGOL) enable programmers to write code in a more human like manner, thereby improving the readability and maintainability of programming.

Operating System: With the development of computers, the operating system has become a key component for managing computing resources and providing programming interfaces. The operating system enables programmers to use higher-level abstractions to write applications without directly dealing with underlying hardware.

Object oriented programming: In the 1970s and 1980s, object-oriented programming (OOP) became popular. The idea of OOP is to encapsulate data and operations in objects, providing a more flexible and modular programming approach.

Internet and Web programming: With the popularization of the Internet, Web programming has become an important field. Technologies such as HTML, CSS, and JavaScript enable developers to create dynamic and interactive web applications.

The rise of the open source and free software movement has brought a new culture of collaboration and sharing to the programming community. Open source software allows developers to freely access, use, and modify code, promoting innovation and technological progress.

Artificial Intelligence and Machine Learning: In recent years, artificial intelligence and machine learning technologies have made tremendous breakthroughs, bringing new challenges and opportunities to the field of programming. These technologies enable computers to learn and make decisions autonomously from data, driving the development of intelligent systems.

Machine language is binary instruction code that computers can directly understand and execute. Among all programming languages, only machine language can be directly recognized by computers. It is a binary sequence of numbers consisting of a series of 0s and 1s, because computers are electronic devices, in their world there are only negative charges (0) and positive charges (1), used to represent the operations and instructions executed by computer hardware. The machine language of each computer is specific, depending on the processor architecture and instruction set used., Machine language is very low-level and difficult to read and write, but it is the foundation of all other high-level programming languages. A compiler or interpreter for high-level languages translates high-level code into machine language instructions, enabling computers to perform corresponding tasks.

2、 Install Compilation Environment

C++belongs to a type of high-level language. As mentioned above, machine language is the foundation of all other high-level programming languages. High level languages require compilers or interpreters to translate high-level code into machine language instructions in order for computers to perform corresponding tasks. So if you want to learn and use C++, you must install a compiler or interpreter. For beginners learning C++compilers, the following are some suitable choices:

1. Code:: Blocks: Code:: Blocks is a free, open-source, and cross platform integrated development environment (IDE) that uses MinGW as the default C++compiler. It provides a simple and easy-to-use interface and basic debugging functions, making it very suitable for beginners.

2. Dev-C++: Dev-C++is also a free, open-source integrated development environment that uses MinGW as the backend compiler. It provides a concise interface and fast compilation speed, suitable for beginners to do simple C++programming.

3. Visual Studio Community Edition: Visual Studio is a powerful and fully functional IDE, and the Community Edition version is free and suitable for individuals and small teams. It provides rich features, including advanced debugging tools and graphical interface design, suitable for more complex C++projects.

4. Xcode: If you are learning C++on a Mac operating system, Xcode is a great choice. It is a free development tool provided by Apple, with a built-in Clang compiler and debugger, suitable for developing iOS and Mac applications.

I personally believe that when scholars first started learning C++programming, Dev-C++was a good choice. As a free and open-source integrated development environment, Dev-C++provides a simple and intuitive interface, making programming easier to get started with. Dev-C++uses MinGW as the default C++compiler, which has fast compilation speed and good compatibility. Can be found in https://www.123pan.com/s/bZ15Vv-bk9sd.html Download and open the compressed file. Go to the Dev Cpp folder and open devccpp.exe to open the compiler. Of course, you can also download it yourself. After opening the compiler, you will see an interface that may have different colors:

If you want to type code, you need to first create a new file, just follow the steps below and click:

Then you can enter this interface and type the code:

3、 Output

Input usually refers to the external data received by the program. These data can come from users, files, sensors, etc. Input can be various forms of information, such as text, numbers, images, etc. The program uses input data to perform corresponding operations or perform specific calculations.

Output is the result or information returned by the program externally. The output can be text, images, sound, files, etc. It is a product obtained by a program after processing input data, used to display results to users or save results to files or other media. To illustrate with a simple example, let's assume we have written a simple calculator program. The user inputs two numbers and an operator through the keyboard, and the program will perform corresponding operations on the input numbers and output the results to the screen. In this example, the numbers and operators entered by the user are the inputs, while the result obtained by the program after calculation is the output.

In C++, you can use cout to output, which is the standard output stream of C++and can output text to the console. You can use the< For example:

#include <bits/stdc++.h>
using namespace std;int main() {cout << "Hello, World!" << endl;//endl是换行return 0;
}

To run code, press this button:

After running, there will be the following effect:

Now you may ask: What kind of code is in the code except for "cout<<" Hello, World! "<

#IncludeAdd a "universal header file". The header file in C++is used to introduce functions, classes, variables, etc. defined in external libraries or other source code files. The header file has an extension of. h or. hpp, usually containing function declarations, macro definitions, structure and class definitions, etc. Simply put, it is to put a library into a program, and there are various types of code in this library for you to use. Different libraries have different code functions, and the universal header file contains the vast majority of commonly used code, so it is called a universal header file, using namespace std; It is a namespace, which is used to prevent function duplication. int main() is the main function in C++programs, and it is the entry point of the program. When the program runs, the operating system will first execute the code in the main function. Return 0; Beginners only need to know that its function refers to closing functions, and; It is a delimiter that must be added after each program segment in C++. Double quotation marks are delimiters used in C++to represent string literals. In the above code, the part enclosed in double quotation marks is "Hello, World!"! "It is a string literal that represents a sequence of characters" Hello, World! "! The string, in simple terms, means that the characters in C++must be enclosed in double quotation marks. If it is only an English character, single quotation marks can be used.

4、 Variables

Variables in C++are containers used to store data. They can be various types, such as integers, floating-point numbers (decimals), characters, strings, etc. In C++, to declare (create) a variable, you need to specify the type and name of the variable. For example, if we want to declare an integer variable, we can use the following syntax:

int n;//int指整数,n指变量名

This will declare an integer variable named n. To assign a value to this variable, the following syntax can be used:

n = 10;

You can also assign values when declaring variables:

int n = 10;

The following are the codes corresponding to different types:

Integer variable (int): used to store integer values. It can store integers in the range of -2147483648 to 2147483647.

Floating point variables (float, double): used to store real values. The float type can store 6-7 significant digits, while the double type can store 15-16 significant digits.

Character type variable (char): used to store a single character.

Boolean variable (bool): used to store Boolean values (true or false 1 or 0).

Short integer variable: used to store smaller integer values. It can store integers in the range of -32768 to 32767.

Long variable: used to store large integer values. It can store integers in the range of -2147483648 to 2147483647.

Unsigned integer variables (unsigned int, unsigned short, unsigned long): used to store positive integer values, excluding negative numbers. The range that can be stored is an integer between 0 and 4294967295.

Long double: A floating-point variable with higher precision than the double type, capable of storing 19-20 significant digits.

Wide character variable (wchar_t): used to store a wide character, usually occupying two or four bytes.

Auto variable: Automatically infer variable type based on initial value.

Register variable: Variables declared as register are stored in CPU registers to speed up program execution.

Extern variable: a global variable defined outside the function and can be used in other files.

Static variables: used to create static variables within a function and globally, which are initialized only once and exist throughout the entire lifecycle of the program.

Const variable: used to represent constants whose values cannot be modified, followed by variable types (int, string...).

5、 Input

If C++wants to input, you can use cin, for example:

#include <bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;cout << n << endl;return 0;
}

After running, this is the effect:

6、 For loop

When scholars first learned C++programming, the for loop was a very important control structure. It is usually used to repeatedly execute a piece of code, allowing the program to iterate according to specified conditions.

The basic syntax of a for loop is as follows:

for (初始化表达式; 条件表达式; 更新表达式) { // 循环体代码 }

Among them:

Initialize expression: Executes before the start of the loop and only once. Usually used to initialize counters or set the initial value of variables.

Conditional expression: Check before each iteration of the loop. If the condition is true (non-zero), continue to execute the loop body; If the condition is false (zero), jump out of the loop.

Update expression: executed after each iteration of the loop, usually used to update counters or change the value of variables.

Here is an example of using a for loop to print numbers 1 to 10:

#include <iostream> 
using namespace std;int main () { for (int i = 1; i <= 10; i++) { cout << i << " "; } return 0; 
}

In this example, we first initialized counter i with int i=1, and then set the conditional expression i<=10, indicating that as long as i is less than or equal to 10, the loop body will continue to be executed. After each iteration of the loop, counter i will perform an auto increment operation through i++. The code cout in the loop body< Used to output the current counter value i, followed by a space. In this way, the program will output numbers 1 to 10, separated by spaces between each number. The for loop can flexibly modify initialization, conditions, and update expressions according to requirements. You can adjust these parts according to specific programming tasks.

这篇关于C++ Algorithm Tutorial (1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/764385

相关文章

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)