C++ Algorithm Tutorial (1)

news2024/11/24 7:45:30

中文版 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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1484490.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

殿堂级Flink源码极精课程预售

一、为什么我们要读源码? 1、让个人技术快速成长: 优秀的开源框架,底层的源码设计思想也非常优秀,同时还有含有大量的设计模式和并发编程技术&#xff0c;优秀的解决方案,熟读源码对猿们技术提升有很大帮助 2、新技术学习能力: Java开源码框架的源码熟读后&#xff0c;若出现…

微服务day03-Nacos配置管理与Nacos集群搭建

一.Nacos配置管理 Nacos不仅可以作为注册中心&#xff0c;可以进行配置管理 1.1 统一配置管理 统一配置管理可以实现配置的热更新&#xff08;即不用重启当服务发生变更时也可以直接更新&#xff09; dataId格式&#xff1a;服务名-环境名.yaml&#xff0c;分组一般使用默认…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的番茄成熟度检测系统(Python+PySide6界面+训练代码)

摘要&#xff1a;开发番茄成熟度检测系统对于提高农业产量和食品加工效率具有重大意义。本篇博客详细介绍了如何利用深度学习构建一个番茄成熟度检测系统&#xff0c;并提供了完整的实现代码。该系统基于强大的YOLOv8算法&#xff0c;并结合了YOLOv7、YOLOv6、YOLOv5的对比&…

【机器学习】CIFAR-10数据集简介、下载方法(自动)

【机器学习】CIFAR-10数据集简介、下载方法(自动) &#x1f308; 个人主页&#xff1a;高斯小哥 &#x1f525; 高质量专栏&#xff1a;Matplotlib之旅&#xff1a;零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程&#x1f448; 希望得到您的订阅和支…

Linux第67步_linux字符设备驱动_注册和注销

1、字符设备注册与注销的函数原型” /*字符设备注册的函数原型*/ static inline int register_chrdev(unsigned int major,\ const char *name, \ const struct file_operations *fops) /* major:主设备号&#xff0c;Limnux下每个设备都有一个设备号&#xff0c;设备号分…

Ainx的全局配置

&#x1f4d5;作者简介&#xff1a; 过去日记&#xff0c;致力于Java、GoLang,Rust等多种编程语言&#xff0c;热爱技术&#xff0c;喜欢游戏的博主。 &#x1f4d7;本文收录于Ainx系列&#xff0c;大家有兴趣的可以看一看 &#x1f4d8;相关专栏Rust初阶教程、go语言基础系列…

[SS]语义分割_膨胀卷积

膨胀卷积 目录 一、概念 1、定义 2、知识点 二、详细介绍 1、引入 2、膨胀系数设定 一、概念 1、定义 膨胀卷积&#xff08;Dilated Convolution&#xff09;&#xff0c;也称为空洞卷积&#xff08;Atrous Convolution&#xff09;&#xff0c;是一种在卷积神经网络…

Vue3 条件渲染 v-show

v-show 指令&#xff1a;用于控制元素的显示或隐藏。 执行条件&#xff1a;当条件为 false 时&#xff0c;会添加一个 display:none 属性将元素隐藏。 应用场景&#xff1a;适用于显示隐藏切换频率较高的场景。 <div v-show"数据">内容</div> 基础用法…

YOLOv9独家原创改进|加入空间和通道重建卷积SCConv模块!

专栏介绍&#xff1a;YOLOv9改进系列 | 包含深度学习最新创新&#xff0c;主力高效涨点&#xff01;&#xff01;&#xff01; 一、本文介绍 本文将一步步演示如何在YOLOv9中添加 / 替换新模块&#xff0c;寻找模型上的创新&#xff01; 适用检测目标&#xff1a; YOLOv9模块…

matlab阶段学习小节1

数组排序 fliplr()实现数组倒序&#xff0c;但不对大小进行排序&#xff0c;只是元素位置掉头。 要想实现大小倒序排列&#xff0c;可以先sort()实现正序排列&#xff0c;再用fliplr倒一下 %数组运算 %矩阵 %xAb的解xb/A;(矩阵) %右除运算A/B&#xff0c;左矩阵为被除数&a…

【嵌入式学习】QT-Day4-Qt基础

简单实现闹钟播报&#xff0c;设置时间&#xff0c;当系统时间与设置时间相同时播报语音5次&#xff0c;然后停止。如果设置时间小于当前系统时间&#xff0c;则弹出消息提示框&#xff0c;并清空输入框。 #include "my_clock.h" #include "ui_my_clock.h&quo…

PHPStudy安装

一、简介 PhpStudy是一款集成了Apache、PHP、MySQL等服务的Web开发环境软件&#xff0c;主要用于本地调试和开发PHP程序。它为用户提供了一个可以简单快捷地搭建PHP运行环境的平台&#xff0c;使得开发者无需手动配置繁琐的服务器环境便可开始开发工作。 1.1 功能 境搭建&am…

音视频开发项目:H.265播放器:视频解码篇

视频演示 如下将演示新版播放器播放 1分钟1080p/25fps/H.265 MP4视频&#xff0c;具体视频参数如下&#xff1a; 粉丝福利&#xff0c; 免费领取C音视频学习资料包学习路线大纲、技术视频/代码&#xff0c;内容包括&#xff08;音视频开发&#xff0c;面试题&#xff0c;FFmpe…

SpringBoot整合rabbitmq-扇形交换机队列(三)

说明&#xff1a;本文章主要是Fanout 扇形交换机的使用&#xff0c;它路由键的概念&#xff0c;绑定了页无用&#xff0c;这个交换机在接收到消息后&#xff0c;会直接转发到绑定到它上面的所有队列。 大白话&#xff1a;广播模式&#xff0c;交换机会把消息发给绑定它的所有队…

编译链接实战(25)ThreadSanitizer检测线程安全

ThreadSanitizer&#xff08;又称为TSan&#xff09;是一个用于C/C的数据竞争检测器。在并发系统中&#xff0c;数据竞争是最常见且最难调试的错误类型之一。当两个线程并发访问同一个变量&#xff0c;并且至少有一个访问是写操作时&#xff0c;就会发生数据竞争。C11标准正式将…

STM32 | J-link安装过程

J-Link是一种由SEGGER公司开发的调试器和仿真器,用于嵌入式系统开发。它可以帮助开发人员在开发过程中进行调试和仿真,提供了快速、稳定的连接,并支持多种不同类型的微处理器和微控制器。 要获取J-Link软件,请访问SEGGER公司的官方网站(www.segger.com)并进入他们的下载…

LLC谐振变换器变频移相混合控制MATLAB仿真

微❤关注“电气仔推送”获得资料&#xff08;专享优惠&#xff09; 基本控制原理 为了实现变换器较小的电压增益&#xff0c;同时又有较 高的效率&#xff0c;文中在变频控制的基础上加入移相控制&#xff0c; 在这种控制策略下&#xff0c;变换器通过调节一次侧开关管 的开关…

现代信号处理学习笔记(二)参数估计理论

参数估计理论为我们提供了一套系统性的工具和方法&#xff0c;使我们能够从样本数据中推断总体参数&#xff0c;并评估估计的准确性和可靠性。这些概念在统计学和数据分析中起着关键的作用。 目录 前言 一、估计子的性能 1、无偏估计与渐近无偏估计 2、估计子的有效性 两个…

【C++】用命名空间避免命名冲突

&#x1f338;博主主页&#xff1a;釉色清风&#x1f338;文章专栏&#xff1a;C&#x1f338;今日语录&#xff1a;如果神明还不帮你&#xff0c;说明他相信你。 &#x1fab7;文章简介&#xff1a;这篇文章是结合谭浩强老师的书以及自己的理解&#xff0c;同时加入了一些例子…

【前端素材】推荐优质后台管理系统网页Stisla平台模板(附源码)

一、需求分析 1、系统定义 后台管理系统是一种用于管理和控制网站、应用程序或系统的管理界面。它通常被设计用来让网站或应用程序的管理员或运营人员管理内容、用户、数据以及其他相关功能。后台管理系统是一种用于管理网站、应用程序或系统的工具&#xff0c;通常由管理员使…