由于需要紧急学习一下C++的一些知识,目前就根据以前其他语言开发的一些思想和流程来简单记录一下C++的处理方式了。
初始开发
开发环境目前我使用的是 Deepin23.1,CPU架构为 amd64,安装了一些相关开发依赖包就可以跑 Hello C++ 了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| sudo apt update sudo apt install -y g++
g++ --version
cd ~/Desktop tee hello.cpp <<EOF #include <iostream> using namespace std;
int main() { cout << "Hello, C++!" << endl; return 0; } EOF
g++ -std=c++17 hello.cpp -o hello
./hello
|
进行交叉编译
目前是需要让程序在 armv7 的物联网盒子上进行运行的,所以这里需要基于已有代码进行交叉编译。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
sudo apt install g++-arm-linux-gnueabihf -y
sudo apt install musl-tools -y
mkdir -p ~/software/musl cd ~/software/musl wget https://musl.cc/arm-linux-musleabihf-cross.tgz tar -zxvf arm-linux-musleabihf-cross.tgz rm -f arm-linux-musleabihf-cross.tgz
~/software/musl/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-g++ --version
cd - ~/software/musl/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-g++ -static hello.cpp -o hello_arm
|
常用代码操作
这里包括了
- 简单IO操作
- 函数定义与实现
- 类的操作
- 列表,集合,map等容器的增删改查与循环的操作
- 多线程与异常相关的处理
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <iostream> #include <vector> #include <map> #include <set> #include <stdexcept> #include <mutex> #include <thread>
int basicIoDemo();
int basicIoDemo() { std::string name; int age; std::cout << "Please input your name and age spilt by a blank." << std::endl; std::cin >> name >> age; if (std::cin.fail()) { std::cout << "Please correct your input!" << std::endl; return -1; } std::cout << "Dear " << name << ", you just tell me you are " << age << "." << std::endl; return 0; }
class Person { private: std::string name; int age; std::string printCurrentTimestamp() { time_t now = time(nullptr); char* dt = ctime(&now); std::string timeStr(dt); if (!timeStr.empty()) { timeStr.erase(timeStr.size()-1); } return "[" + timeStr + "]"; } public: Person(std::string n, int a) { name = n; age = a; } ~Person() { std::cout << "I will destroy the resources after the object is destroyed" << std::endl; } void sayHello() { std::cout << printCurrentTimestamp() << " Hello, I'm " << name << ", " << age << " years old." << std::endl; } static void printInfo() { std::cout << "Person is a word in English." << std::endl; } std::string getName() { return name; } void setAge(int a) { age = a; } }; void personDemo() { Person p("Easul", 12); p.sayHello(); p.setAge(20); p.sayHello(); }
void containerDemo() { std::vector<int> vec = {2, 2, 3, 4, 5}; vec.push_back(12); for (int i = 0; i < vec.size(); i++) { std::cout << vec[i] <<std::endl; } std::cout << "==================" <<std::endl; vec.pop_back(); for (int n: vec) { std::cout << n <<std::endl; }
std::map<std::string, int> ages; ages["Easul"] = 21; ages["Alice"] = 18; ages["John"] = 16; for (auto &pair : ages) { std::cout << pair.first << "=>" << pair.second << std::endl; } std::string name = "Stevie"; if (ages.find(name) == ages.end()) { std::cout << "未找到" << name << std::endl; } ages.erase("Alice");
std::set<std::string> fruits = {"apple", "grapes", "banana"}; fruits.insert("banana"); for (auto &fruit : fruits) { std::cout << fruit << std::endl; } fruits.erase("apple"); for (auto &fruit : fruits) { std::cout << fruit << std::endl; } if (fruits.count("banana") > 0) { std::cout << "You can get banana here." << std::endl; } }
std::mutex countMutex; void task(int id, int limit) { try { for (int i = 0; i < limit; i++) { if (i == 3 && id == 2) { throw std::runtime_error("Task 2 encountered an error when id equals 2"); } { std::lock_guard<std::mutex> lock(countMutex); std::cout << "Task " << id << ": i = " << i << std::endl; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } catch (const std::exception &e) { std::lock_guard<std::mutex> lock(countMutex); std::cerr << "Exception in task " << id << ": " << e.what() << std::endl; } } void runTaskDemo() { std::vector<std::thread> threads; for (int i = 0; i < 3; i++) { threads.emplace_back(task, i, 10); } for (auto &t : threads) { t.join(); } }
int main() { return 0; }
|