Notice:
1. Functions defined in C + + should have destructors
2. The custom header file in C + + needs
#ifndef CODE_CPP_GLOBAL_H #define CODE_CPP_GLOBAL_H #endif //CODE_CPP_GLOBAL_H
3. When inserting key value pairs into the map in C + +, you must first check whether the key exists, because:
-a. if you insert the same key, no error will be reported at compile time and run time
-b. if the key value of the inserted data already exists, the insertion operation will be ignored
About C++
1. Constructor and destructor of class~
Rookie tutorial: constructor and destructor of C + + class
2. heavy haul
(1) overloaded operators and overloaded functions
(2) I / O operator overload
3. Quotation summary
https://www.cnblogs.com/jycboy/p/5184638.html
4.double rounded to int
double qty; int i_qty = (int) (qty + 0.5);
5. sort function call
(1) https://www.cnblogs.com/jjzzx/p/5122381.html
(2) use the iterator (reverse iterator) to sort in order (reverse order). The default is from small to large:
Order: #include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { string str("hello world"); sort(str.begin(),str.end()); cout<<str; return 0; } //Result: the space dehllorw
Reverse order: #include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { string str("hello world"); sort(str.rbegin(),str.rend()); cout<<str; return 0; } //Result: wroollhde space
6. Keyword auto
auto can automatically select the matching type for this variable according to the type of initial value of the variable when declaring the variable, which is used to replace the lengthy and complex variable declaration with a specific range of variables.
7.map traversal
For a map, the following two lines of loops are wrong, and the map cannot be traversed as follows with i:
for(int i=0; i<map.size(); i++) TaskInfo* pInfo = map[i];
A proper traversal:
map<char,string> mp; map<char,string>::iterator it; int main(int argc, char const *argv[]) { mp['0']="0000";mp['1']="0001";mp['2']="0010"; mp['3']="0011";mp['4']="0100";mp['5']="0101"; mp['6']="0110";mp['7']="0111";mp['8']="1000"; mp['9']="1001";mp['A']="1010";mp['B']="1011"; mp['C']="1100";mp['D']="1101";mp['E']="1110"; mp['F']="1111"; for(it = mp.begin(); it != mp.end(); it++) cout<<it->first<<":"<<it->second<<endl; //It - > the value of the first subscript, it - > the value of the second subscript return 0; }
File input and output
(1)
The content of Benchmark.txt is as follows:
620000044158676000 1 A10 33.200520833333336 620000044135169000 9 A11 49.76450704225352 620000044128782000 1 A02 35.15409035409036 620000044128968000 1 A02 59.412454212454215 620000044129080000 1 A02 56.10329670329671 620000044130416000 1 A14 71.11111111111111 620000044126894000 2 A09 42.328711528429835 620000044126899000 2 A03 66.66666666666667 620000044136505000 1 A08 42.30769230769231 620000044136571000 1 A08 46.36923076923077 620000044137207000 1 A12 16.768559272300468 620000044131144000 2 A03 60.827481481481485 620000044132226000 2 A13 78.73167701863353 620000044139178000 4 A08 53.53641025641026 620000044162027000 1 A02 68.33191697191697 620000044106464000 3 A09 71.75880020865937
C++ Code:
#include <iostream> #include <fstream> #include <string> #include <cassert> #include <map> using namespace std; class problem{ public: problem(){}; ~problem(){}; public: std::string oid; int qty; std::map<int, int> num; std::string benchmark_sol_carton; double benchmark_sol_utl; }; int main() { std::map<std::string, problem> map_order; std::map<std::string, problem>::iterator it; string filename = "Benchmark.txt"; ifstream fin(filename.c_str()); string line; // output lines in Benchmark.txt while(getline(fin, line)){ cout<<line<<endl; } fin.close(); // set value for map_order fin.open(filename.c_str()); if(fin.fail()){ std::cerr << "File open failed: " + filename << std::endl; } std::string or_id; while(fin >> or_id){ problem & o = map_order[or_id]; // if map_order[or_id] doesn't exsit, fin >> o.qty; fin >> o.benchmark_sol_carton >> o.benchmark_sol_utl; } fin.close(); // check the value of map_order for (it = map_order.begin(); it != map_order.end(); ++it) cout << it->first << " = " << it->second.qty << endl; return 0; }
Operation result:
620000044158676000 1 A10 33.200520833333336 620000044135169000 9 A11 49.76450704225352 620000044128782000 1 A02 35.15409035409036 620000044128968000 1 A02 59.412454212454215 620000044129080000 1 A02 56.10329670329671 620000044130416000 1 A14 71.11111111111111 620000044126894000 2 A09 42.328711528429835 620000044126899000 2 A03 66.66666666666667 620000044136505000 1 A08 42.30769230769231 620000044136571000 1 A08 46.36923076923077 620000044137207000 1 A12 16.768559272300468 620000044131144000 2 A03 60.827481481481485 620000044132226000 2 A13 78.73167701863353 620000044139178000 4 A08 53.53641025641026 620000044162027000 1 A02 68.33191697191697 620000044106464000 3 A09 71.75880020865937 620000044106464000 = 3 620000044126894000 = 2 620000044126899000 = 2 620000044128782000 = 1 620000044128968000 = 1 620000044129080000 = 1 620000044130416000 = 1 620000044131144000 = 2 620000044132226000 = 2 620000044135169000 = 9 620000044136505000 = 1 620000044136571000 = 1 620000044137207000 = 1 620000044139178000 = 4 620000044158676000 = 1 620000044162027000 = 1 Process finished with exit code 0