C++

C++学习笔记📒

Posted by Elli0t on 2020-10-19

Enrich your life today, yesterday is history, tomorrow is mystery.

第一次用 xcode 写代码,感觉还是不错的。✌️界面简洁美观,功能还没怎么摸全,先慢慢用着。不错的

sadfasf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// main.cpp
// C++
//
// Created by Elli0t_Logic on 2020/10/15.
//

#include <iostream>
#include <string>
#include <unistd.h>

/*
C++中,string头文件基本上bai已经包含在iostream中了
但是,平时使用的时候建zhi议加上#include<string.h>(尤其在以下情况下)
1、使用string类型
2、使用cin、cout语句来输入输出string类型变量(注意,同时还需要#include<sstream>)
3、使用memset()、strlen()、strcpy()等函数时
*/

using namespace std;

/* The first time write C++ */

void ret_who(){
cout << "who are you?" << endl;
}

int main(int argc, const char * argv[]) {
// insert code here...
const string my_name = "Elli0t";
string your_name, sentence, txt, answer;
char number_a = 97;
int a = 4, b = 0;
double d_sleep = 1;
bool c_if;
cout << "a: Hello, I'm " << my_name << ".";
ret_who();
sleep(d_sleep);
cout << "b: ";
your_name = "Tim";
c_if = 0;
if ( c_if ){
cin >> your_name;
// cin considers a space (whitespace, tabs, etc) as a terminating character.
}else cout << your_name << endl;
cout << "a: How are you ," << your_name << ". I tell you the ASCII of " << number_a << " is 97.\n";
sleep (d_sleep);
cout << "b: I'm OK! and I tell you C++ some operators." << endl;
sleep (d_sleep);
b += ++a;
cout << " a = 4, b = 0, b += ++a , b = " << b << endl;
sleep (d_sleep);
a = 4;
b = 0;
b += a++;
cout << " a = 4, b = 0, b += a++ , b = " << b << endl;
sleep (d_sleep);
cout << " Bacause x += ++i will increment i and add i+1 to x. x += i++ will add i to x, then increment i.\n";
sentence = "a: ye, and I know some ";
sentence.append("tips too.\n");
sleep (d_sleep);
cout << sentence;
txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << " txt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n";
cout << " The length of the txt string is: " << txt.length() << endl;
sleep (d_sleep);
cout << "b: ";
getline (cin, answer);
return 0;
}

文件读写

https://www.runoob.com/cplusplus/cpp-files-streams.html

空格为每次读取的间断标志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>

namespace struct_pople {
struct people{
int id = 0;
char name[20] = {0};
}p[30];
}
using namespace std;
int main(){
ifstream infile;
infile.open("name.txt");
cout << "reading from the file" << endl;
int num = 0;
for(int i = 0; infile; i++){
infile >> num;
if (num == 0) {
break;
}
struct_pople::p[i].id = num;
infile >> struct_pople::p[i].name;
}
infile.close();
for (int i = 0; struct_pople::p[i].id != 0; i++) {
cout << struct_pople::p[i].id << struct_pople::p[i].name << endl;
}
return 0;
}

Link

https://www.w3schools.com/cpp/default.asp

str to hex

https://tool.lu/hexstr/