C ++ 프로그래밍 기초 노트

1 간단한 C를 작성 ++ 프로그램

int main()
{
    return 0; /*标志着一个程序的结束*/
}
/*
一个函数包含以下几个部分:
    返回类型 函数名称(形参列表){
        函数体;
    }
*/

 입력 및 출력 2

#include <iostream>
using namespace std;/*在前面加上这一句,之后的代码就不用频繁的加"std::"了,可直接写cin、cout、endl*/
/*
  输入(常用输入语句):cin、scanf、getchar、getline
  输出(常用输出语句):cout、printf
*/
int main()
{
/*一、输出*/
    int i=10;
    char ch = 'h';
    std::cout << "hello,world" << std::endl << i << ch << std::endl; /*用<<连接所要输出的字符串,std::endl表示是一个换行符*/
                                                                     //endl的全称是"end of line"
    int j = 16;
    printf("%d\n",j); /*输出十进制数j*/
    printf("%x\n",j); /*输出十六进制数j*/
    printf("%#x\n",j); /*通常在x前面加一个#号,输出时会显示0x表示一个十六进制的数。*/
    printf("%4d\n",j); /*表示j的数据宽度是4位,前面补空格*/
    printf("%04d\n",j); /*表示j的数据宽度是4位,前面补0*/

    printf("%f\n",10.0); /*输出浮点型数据*/
    printf("%.1f\n",10.0); /*输出浮点型数据,保留一位小数*/
    printf("%6.1f\n",10.0); /*输出浮点型数据,保留一位小数,且数据宽度为6位,前面补空格*/

    printf("%c\n",ch); /*输出单个字符*/
    printf("%s\n","huihui"); /*输出字符串*/

/*二、数据输入与输出:*/
    std::cin >> i >> j;
    std::cout << i << j << std::endl;

    scanf("%d %d",&i,&j);
    std::cout << i << j << std::endl;

    scanf("%d,%d",&i,&j); /*scanf与cin的区别:输入数据时scanf可用逗号间隔开数据加以控制,而cin不容易显示*/
    std::cout << i << j << std::endl;


/*三、字符的输入与输出:*/
    ch = getchar(); /*输入单个字符*/
    cout << ch << endl;
    
    getchar(); /*加这一句是为了把上句结束之后的回车吃掉,以紧接着开始下一句代码的输入操作,否则执行完上一句后会有回车无法承接下一句*/
    string s;
    getline(cin,s); /*输入字符串s*/
    cout << s << endl;

    return 0;
}

3 파일 이름 공간

#include <iostream> //输入输出流cin、cout        //注:stdio 是C标准库里面的函数库,对应的基本都是标准输入输出等等C语言常用库的定义。iostream 是C++标准库的头定义,对应的基本上是C++的输入输出相关库定义。开发C程序用Stdio, C++用Stdio/iostream 都可以。
#include <algorithm> //一些简单函数,如equal、swap、sort、min、max
#include <vector> //容器可当做数组使用
#include <string> //可变长字符序列
#include <stack> //栈
#include <queue> //队列
#include <map> //键值对
#include <set> //集合
#include <math.h> //包含pow、sqrt、ceil、floor、abs
using namespace std; //命名空间(注:在大型工程中有很多相同的函数、相同的名称,区别它们的方法就是加上各自的命名空间(相当于目录),对号入座,那么整个程序就不会崩溃。需要告诉编译器你所调用的是哪个域下的以及哪个命名空间下的函数)

int main(){
    cout << "Hello,world!" << endl;
    system("pause"); //让程序在此处暂停,以防止程序立马结束无法查看输出结果
                     //也可用getchar();
    return 0;
}

4 선언 함수 및 변수의 정의

#include <iostream>
#include <algorithm> 
#include <vector> 
#include <string>
#include <math.h>
using namespace std; 

//函数的声明:(只能放在函数(如main函数)的前面)
void sum(int a, int b);

//函数的定义:(可放在函数前也可放在函数的后面)
void sum(int a, int b){
}


int main()
 {
   sum(1,2);
 //变量的声明与定义(注意:以下三种情况需分别执行来看结果,其余ctrl+k+c注释掉,ctrl+k+u可变回代码):
  //case1:
   extern int i; //仅声明一个变量i
   cout << i << endl; //会提示错误
  //case2:
   int i;              //声明并定义i,但不提倡
   cout << i << endl;  //会输出0,并提示警告,变量未初始化
     /*
        未初始化的变量含有一个不确定的值
        使用未初始化变量的值是一种严重错误的编程行为并且很难调试
        尽管大多数编译器都能对一部分使用未初始化变量的行为提示警告,但严格来说,编译器并未要求检查此类错误
        所以,请养成良好习惯,声明定义变量必初始化!
     */
  //case3:
   int i=0; //声明并定义变量i,并初始化为0,提倡
   cout << i << endl;

   system("pause");
   return 0;
 }

//函数的定义:
// void sum(int a, int b){}

5 주 기능

//记住一点:main函数是程序的唯一入口!没有它整个程序就没有入口,无法执行!
int main(){ //{}包起来的叫做代码块

    return 0;
}

6 문 동안

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

int main(){
    int sum = 0,val = 1;
    while(val <= 10){
        sum += val;
        val++;
    }
    cout << sum << endl;
    
    system("pause");
    return 0;
}

 문 7

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

int main(){
  //用法一:
    int sum = 0;
    for(int i = 0;i <=10;i++){
        sum += i;
    }
    cout << sum << endl;
  //用法二:
    sum=0;
    vector<int> v{1,2,3,4,5,6,7,8,9,10};
    for(auto i: v){          //i会依次遍历数组v中的每一个值
        sum += i;
    }
    cout << sum << endl;

    system("pause");
    return 0;
}

8 if 문

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
/*
Input: 42 42 42 42 42 55 55 62 100 100 100^z
Output:
        42 occurs 5 time
        55 occurs 2 time
        62 occurs 1 time
        100 occurs 3 time
*/
int main()
{
    int currval = 0,val = 0;
    if (cin >> currval)
    {
        int count = 1;
        while (cin >> val)
        {
            if (currval == val)
            {
                count++;
            }
            else
            {
                cout << currval << "occurs" << count << "times" << endl;
                currval = val;
                count = 1;
            }
        }
        cout << currval << "occurs" << count << "times" << endl;
    }
    system("pause");
    return 0;
}

 검토 요약 :

/*
首先我们知道一个C++程序是由一个或者多个函数构成,其中必须包含一个main函数,
main函数是一个程序的入口,没有main函数,程序将无法运行。


输入与输出:常用的输入语句有:cin、 scanf、 getchar、 getline
常用的输出语句有:cout、 printf
他们各有各的特点,譬如cin、cout从 stream(流)中接受数值,自动匹配类型。
scanf和printf可以控制输入输出格式,
scanf可以读入以逗号为间隔的数值,
printf可以控制数据宽度、进制、前缀后缀。 //前缀即在不足数据宽度的地方补0;后缀是在输出浮点数时对小数位数进行控制。
getchar从输入流接受一个字符,getline从输入流接受一行字符串,他们都将空格和回车视为字符。
不同的是,getchar会把回车留在输入流中,而 getline不会。 //getchar读出一个字符后,以回车作为结束符,它会把回车留在输入流中作为下一次的输入;getline会把回车作为结束符但回车符不会留在输入流中。


声明必须在调用之前(对函数而言),声明定义变量,最好进行初始化。


While语句,for语句,if语句,即使他们的作用域中只有一条语句,依然建议
使用大括号进行包裹,熟练使用它们就可以编写一些简单的算法题
*/

 (9)가 문을 계속 휴식을 전환 (예 1)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

/*
1.统计各个元音字母出现的次数并输出
Input: huihuikaoyanzhuninchenggong
Output:
        Number of vowel a: 2
        Number of vowel e: 1
        Number of vowel i: 3
        Number of vowel o: 2
        Number of vowel u: 3

程序逻辑:
①从输入内容中读取所有字符
②令每个字符都与元音字母的集合进行比较
③如果字符与某个元音字母匹配,将该字母的数量加1
④输出统计结果
*/

/*
基本逻辑结构:
switch(ch):
    case 'a':
        acnt++;
        break;
    case 'e':
        ecnt++;
        break;
    case 'i':
        icnt++;
        break;
    case 'o':
        ocnt++;
        break;
    case 'u':
        ucnt++;
        break;
*/

int main()
{
    char ch;

    int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;

    //一直读入字符,知道遇到回车符号
    while ((ch = getchar()) != '\n')
    {
        //break;
        //continue;//如果这里加continue或者break,则程序陷入死循环
        switch (ch) //对ch进行匹配
        {
        case 'a':
            ++aCnt;
            break;//返回进行下一次while,下同
            // continue;//返回进行下一次while,下同.这里加break或continue作用相同
        case 'e':
            ++eCnt;
            break;
            // continue;
        case 'i':
            ++iCnt;
            break;
            // continue;
        case 'o':
            ++oCnt;
            break;
            // continue;
        case 'u':
            ++uCnt;
            break;
            // continue;
        }
    }
    //输出元音字母的个数
    cout << "Number of vowel a: " << aCnt << '\n';
    cout << "Number of vowel e: " << eCnt << '\n';
    cout << "Number of vowel i: " << iCnt << '\n';
    cout << "Number of vowel o: " << oCnt << '\n';
    cout << "Number of vowel u: " << uCnt << '\n';
    system("pause");
    return 0;
}

 (9)가 문을 계속 휴식을 전환 (예 2)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

/*
2.统计元音字母的总个数
Input: huihuikaoyanzhuninchenggong
Output:
        Number of vowel : 11
*/

int main()
{
    char ch;
    int vowelCnt = 0;

    while ((ch = getchar()) != '\n')
    {
        switch (ch)
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            ++vowelCnt;
            break;
        }
    }
    cout << "Number of vowel : " << vowelCnt << '\n';
    system("pause");
    return 0;
}

 (9)가 문을 계속 휴식을 전환 (예 3)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

/*
3.统计元音字母和非元音字母出现的次数并统计输出
Input: huihuikaoyanzhuninchenggong
Output:
        Number of vowel : 11
        Number of non-vowel : 16
*/

int main()
{
    char ch;
    int vowelCnt = 0, otherCnt = 0;

    while ((ch = getchar()) != '\n')
    {
        switch (ch)
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            ++vowelCnt;
            break;
        default:
            ++otherCnt;
            break;
        }
    }
    cout << "Number of vowel : " << vowelCnt << '\n';
    cout << "Number of non-vowel : " << otherCnt << endl;
    system("pause");
    return 0;
}

 10 개 클래스 소개

/*
题目:给你一个整数数组nums,请你返回其中位数为 偶数 的数字的个数。

示例1:
 输入:nums = [12,345,2,6,7896]
 输出:2
 解释:
 12 是 2 位数字(位数为偶数)
 345 是 3 位数字(位数为奇数)
 2 是 1 位数字(位数为奇数)
 6 是 1 位数字(位数为奇数)
 7896 是 4 位数字(位数为偶数)
 因此只有 12 和 7896 是位数为偶数的数字

示例2:
 输入:nums = [555,901,482,1771]
 输出:1
 解释:
 只有 1771 是位数为偶数的数字

提示:
1 <= nums.length <= 500
1 <= nums[i] <= 10^5
*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

class Solution
{
public:
    int findNumbers(vector<int> &nums)
    {
        int count = 0,count_evev = 0;
        for (int i = 0;i < nums.size();i++)
        {
            while (nums[i] != 0)
            {
                nums[i] /= 10;
                count++;
            }
            if (count % 2 == 0)
            {
                count_evev++;
            }
            count = 0;//重置count
        }
        return count_evev;
    }
};

//用main函数实例化一个类,对上面定义的类进行验证
int main() 
{
    Solution s;//实例化一个类s

    vector<int> num = {437,315,322,431,686,264,442};

    int count_even = s.findNumbers(num);
    printf("%d\n",count_even);
    system("pause");
    return 0;
}

11 참조 포인터 값은 전달

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

//1.值传递
void swap1(int a,int b)
{
    int t = a;//定义一个临时变量t保存a的值
    a = b;
    b = t;
    printf("swap1,a b address: %#x %#x\n", &a, &b);
}

//2.指针传递
void swap2(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
    printf("swap2,a b address: %#x %#x\n", a, b);
}

//3.引用传递
void swap3(int &c, int &d)
{
    int t = c;
    c = d;
    d = t;
    printf("swap3,a b address: %#x %#x\n", &c, &d); 
}

int main()
{
    int a = 1,b = 2;
//case1:
    swap1(a,b);
    cout << "swap1:" << a << " " << b << endl;
    cout << "a b address: " << &a << " " << &b << endl;       //结论:采用值传递交换两个数值的想法不成立(可看到执行结果中交换后a和b的地址发生了改变)

//case2:
    // swap2(&a,&b);
    // cout << "swap2:" << a << " " << b << endl;
    // cout << "a b address: " << &a << " " << &b << endl;    //结论:交换是成功的,main函数中a、b交换后的地址与前面swap3的地址是相同的,说明可行

//case3:
    // swap3(a,b);
    // cout << "swap3:" << a << " " << b << endl;
    // cout << "a b address: " << &a << " " << &b << endl;    //结论:交换也是成功的
    
    system("pause");
    return 0;
}

(12) 어레이 (벡터의 사용은 더 큰 배열을 나타낸다)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h> //注意:此处string.h是C标准库下的头文件,与string有本质的区别,若改为string则会出现错误
#include <math.h>
using namespace std;

#define size1 5
const int size2 = 100;

int main()
{
    //定长一维数组声明及定义:
    int array1[10000];
    int array2[10000] = {1, 2, 3, 4, 5, 6};
    //int array3[]; //非法,必须指定长度
    
    //定长二维数组声明及定义:
    int array4[100][100];
    int array4_[100][100] = {{0,0}, {0,1}, {2,0}};

    int array5size = 0;
    scanf("%d", &array5size);
    int array5[array5size]; //有些编译器不支持接受变量作为数组长度
    int array6[size1];
    int array6_[size2];

    //接受一个变量作为数组长度的一种解决方法:
    int *nums1 = (int *)malloc(array5size * sizeof(int)); //未进行初始化,数组中的值为烂值
    nums1[0] = 1;
    for (int i = 0; i < array5size; i++)
    {
        nums1[i] = 0;
        printf("%d\n",nums1[i]);
    }

    int m, n; //m是列,n是行
    scanf("%d %d", &m, &n);
    int **nums2 = (int **)malloc(m * sizeof(int *));
    for (int i = 0; i < n; i++)
    {
        nums2[i] = (int *)malloc(n * sizeof(int));
    }

    nums2[0][0] = 1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            nums2[i][j] = 0;
            printf("%d",nums2[i][j]);
        }
        printf("\n");
    }

    //字符
    char ch;
    //字符串
    char *s = "huihuikaoyan";
    //字符数组
    char c1[13] = "huihuikaoyan"; //长度必须比定义的字符串长度要长(至少长一位),因为字符串以\0为末尾。(如此例huihuikaoyan长度为12但数组长度为13)
    //字符串一维、二维数组
    char *c2[100];
    char *c3[100][100];

    for (int i = 0; i < strlen(s); i++)
    {
        printf("%c", s[i]);
    }
    printf("\n%s\n", c1);

    if (c1[12] == '\0') //数组下标是从0开始的(0~12)
    {
        printf("Yes\n");
    }

    system("pause");
    return 0;
}

//注:终端输入验证看直播录像1_13视频(开始时间36:09)

13 형식 변환

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

int main()
{
    //字符串转数字
    string s = "100";
    int num1 = stoi(s); //可使用stoi将一个 字符串类型 的变量转化为一个 int型 变量

    cout << num1 << endl;

    s = "100.111";
    double num2 = stod(s); //可使用stod将一个 字符串类型 的变量转化成一个 double类型 的变量

    cout << num2 << endl;

    //数字转字符串
    string s1 = to_string(100); //可使用to_string把一个数字转化为字符串
    cout << s1 << endl;
    string s2 = to_string(100.111);
    cout << s2 << endl;

    //string类型转为const char*类型用printf输出
    auto s3 = s2.c_str(); //可用c_str将一个string类型转化为const char*类型变量(const char*表示一个限定不会被改变的指针变量,地址与值均不改变;而char*是常量指针,地址不可以改变,但是指针的值可变)
    printf("%s\n",s3);

    //大小写字符转换(口诀:大小小大。大写字母的ASCII值小,小写字母的ASCII值大)
    string s4 = "A"; 
    s4[0] += ('a'-'A'); //ASCII码值a为65,A为97。'a'-'A'=32
    cout<<s4<<endl;
    
    system("pause");
    return 0;
}

 

출시 일곱 개 원래 기사 · 원 찬양 한 · 전망 (284)

추천

출처blog.csdn.net/qq_45599068/article/details/104082977