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.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

electron+vue3全家桶+vite项目搭建【28】封装窗口工具类【2】窗口组,维护窗口关系

文章目录 引入实现效果思路主进程模块渲染进程模块测试效果 引入 demo项目地址 窗口工具类系列文章&#xff1a; 封装窗口工具类【1】雏形 我们思考一下窗口间的关系&#xff0c;窗口创建和销毁的一些动作&#xff0c;例如父子窗口&#xff0c;窗口组合等等&#xff0c;还有…

labview数组精讲

题主经过写文章一段时间的发现,许多同学对该软件的理解和编程能力是不太一样的,有些知识相对一些同学较为简单,但是有些同学提问就比较困难。那么针对这个问题,题主打算出一期说白话系列的专栏,在该栏目中用最通俗的大白话和例子去让大家深刻了解这个软件的功能和摸透他的…

【center-loss 中心损失函数】 原理及程序解释(更新中)

文章目录 前言问题引出open-set问题抛出 解决方法softmax函数、softmax-loss函数解决代码&#xff08;center_loss.py&#xff09;原理程序解释 代码运用 如何梯度更新首先了解一下基本的梯度下降算法然后 补充&#xff1a;外围知识模型 前言 学习一下&#xff1a; 中心损失函…

报错问题解决django.db.utils.OperationalError: (1049, “Unknown database ‘ mxshop‘“)

开发环境&#xff1a;ubuntu22.04 pycharm 功能&#xff1a;django连接使用mysql数据库&#xff0c;各项配置看似正常 报错&#xff1a; django.db.utils.OperationalError: (1049, "Unknown database mxshop") 分析检查原因&#xff1a; Setting的配置文件内&…

分布式版本控制系统 git

学习目标&#xff1a; 提示&#xff1a;了解及 学会使用 git 1、 含义&#xff1a; Git&#xff08;读音为/gɪt/&#xff09;是一个 开源的分布式版本控制系统 &#xff0c;可以有效、高速地处理从很小到非常大的项目版本管理。 git 也是Linus Torvalds为了帮助管理Linux内核…

信号系统之快速傅里叶变换

1 使用复数DFT的实数DFT 本文的主题&#xff0c;如何使用 FFT 计算真正的 DFT&#xff1f; 由于 FFT 是一种用于计算复数 DFT 的算法&#xff0c;因此了解如何将实数 DFT 数据输入和输出复数 DFT 格式非常重要。图 12-1 比较了实数 DFT 和复数 DFT 存储数据的方式。实数 DFT …

VUE3 页面路由 router

VUE3 页面路由 router 1.理解路由流程1.1新建工程1.2 理解路由1.3 结果1.4补充 2.路由传递参数2.1.新建工程2.2.打开项目2.3.新建路由2.4 路由传递参数 3.路由嵌套配置3.1 基础3.2 子二级布局文件3.3 配置子二级路由3.4 父级页面链接路由3.5 结果3.6 细节&#xff1a;子二级目录…

String类的使用

String常用的构造方法 String的源码 内部是一个数组和hash值&#xff0c;涉及到常量池后续补充&#xff08;常量池&#xff1a;存储相同的字符时只会存储一租&#xff09; String的比较 equals()与&#xff1a;String里面为我们提供了许多方法&#xff0c;可直接调用&#xf…

Rocky Linux 运维工具 firewall-cmd

一、firewall-cmd​的简介 ​​firewall-cmd​是基于firewalld的防火墙管理工具。用户可以使用它来配置、监控和管理防火墙规则&#xff0c;包括开放端口、设置服务规则等。 二、firewall-cmd​​的参数说明 序号参数描述1​​–zone指定防火墙区域2–add-portxxx/tcp允许特定…

spring boot3解决跨域的几种方式

⛰️个人主页: 蒾酒 &#x1f525;系列专栏&#xff1a;《spring boot实战》 &#x1f30a;山高路远&#xff0c;行路漫漫&#xff0c;终有归途。 目录 1.前言 2.何为跨域 3.跨域问题出现特征 4.方式一&#xff1a;使用 CrossOrigin 注解 5.方式二&#xff1a;自定义…

erp项目采购模块新增商品,商品价格计算demo

1&#xff1a;因为此前erp项目中的采购模块添加商品&#xff0c;实时计算单个商品预估价格及全部商品总额折磨了我很久&#xff0c;所以今天闲来无事优化一下&#xff0c;使用另一种思维来做一个类似demo&#xff0c;作为此前总结反思 2&#xff1a;原来项目模块是这样的 3&am…

达梦数据库DM8安装与配置

达梦数据库安装与配置 1 安装前准备 1.1 新建 dmdba 用户 创建用户所在的组&#xff0c;命令如下&#xff1a; groupadd dinstall创建用户&#xff0c;命令如下&#xff1a; useradd -g dinstall -m -d /home/dmdba -s /bin/bash dmdba修改用户密码&#xff0c;命令如下&am…

(每日持续更新)jdk api之OutputStreamWriter基础、应用、实战

博主18年的互联网软件开发经验&#xff0c;从一名程序员小白逐步成为了一名架构师&#xff0c;我想通过平台将经验分享给大家&#xff0c;因此博主每天会在各个大牛网站点赞量超高的博客等寻找该技术栈的资料结合自己的经验&#xff0c;晚上进行用心精简、整理、总结、定稿&…

MyBatis 学习(五)之 高级映射

目录 1 association 和 collection 介绍 2 案例分析 3 一对一关联和一对多关联 4 参考文档 1 association 和 collection 介绍 在之前的 SQL 映射文件中提及了 resultMap 元素的 association 和 collection 标签&#xff0c;这两个标签是用来关联查询的&#xff0c;它们的属…

中小企业“数智未来”行动|ZStack Cloud 荣获“推荐方案”奖

2月29日&#xff0c;以“数智未来 共创数字时代新篇章”为主题的中小企业“数智未来”行动在京成功举办&#xff0c;本次活动由中央广播电视总台央视频和中国中小企业协会作为联合观察单位&#xff0c;带来了一系列帮助中小企业成就业务新价值和数智化升级的优秀产品和方案&…

从入门到精通的Android进阶学习笔记整理,你有过迷茫吗

面试题 一般Android面试分为两部分&#xff1a;Java部分和Android部分&#xff0c;下面说一下自己面试过程遇到的一些具体题目和一些相关知识点。 一 JAVA相关 1&#xff09;JAVA基础 1.java基本数据类型有哪些&#xff0c;int&#xff0c; long占几个字节 2. 和 equals有什…

Mint_21.3 drawing-area和goocanvas的FB笔记(二)

一、goocanvas安装 Linux mint 21.3 库中带有 libgoocanvas-2.0-dev, 用sudo apt install libgoocanvas-2.0-dev 安装&#xff0c;安装完成后&#xff0c;检查一个 /usr/lib/x86_64-linux-gnu 下是否有libgoocanvas.so的软件链接。如果没有&#xff0c;或是 .so.x 等类似后面…

QT Widget: 自定义Widget组件及创建和使用动静态库

学习自定义Widget组件&#xff0c;书中的案例&#xff1a; // 自定义QmyBattery组件 // QmyBattery.c #include "qmybattery.h"QmyBattery::QmyBattery(QWidget *parent) : QWidget(parent) {}/** 1.QPainter的viewport()与window()分别代表着物理坐标与逻辑坐标区域…

类加载的基本流程

⭐ 作者&#xff1a;小胡_不糊涂 &#x1f331; 作者主页&#xff1a;小胡_不糊涂的个人主页 &#x1f4c0; 收录专栏&#xff1a;JavaEE &#x1f496; 持续更文&#xff0c;关注博主少走弯路&#xff0c;谢谢大家支持 &#x1f496; 类加载 1. 加载2. 验证3. 准备4. 解析5. 初…

maven项目报错Cannot resolve plugin org.apache.maven.plugins:maven-war-plugin:2.2

如果IDEA整合maven没有问题&#xff0c;还是报这个错误&#xff0c;很大可能是由于在下载过程中存在网络问题&#xff0c;导致文件下载一半而停止&#xff0c;但是已经在仓库中存在这个文件夹&#xff0c;解决方法是删除文件夹重新下载即可。 删除本地仓库下的\org\apache\mav…