coding边学边记之C++

Notice:

1.C++中定义函数要有析构函数
2.C++中自定义头文件需要

#ifndef CODE_CPP_GLOBAL_H
#define CODE_CPP_GLOBAL_H

#endif //CODE_CPP_GLOBAL_H

3.C++中的map插入键值对时,一定要先检查键是否存在,因为:
-a.如果插入相同键的操作,无论是编译和运行时都不会报错
-b.对于插入的数据的键值已存在,那么这个插入操作将会被忽略

About C++

1. 类的构造函数和析构函数~

菜鸟教程:C++类的构造函数和析构函数

2.重载

(1)重载运算符和重载函数
(2)输入/输出运算符重载

3.引用总结

https://www.cnblogs.com/jycboy/p/5184638.html

4.double四舍五入为int

double qty;
int i_qty = (int) (qty + 0.5);

5. sort函数调用

(1) https://www.cnblogs.com/jjzzx/p/5122381.html
(2)使用迭代器(反向迭代器)可进行顺序(逆序)排序,默认是从小到大:

顺序:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    string str("hello world");
    sort(str.begin(),str.end());
    cout<<str;
    return 0;
 } 

结果:空格dehllloorw
逆序:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
   string str("hello world");
   sort(str.rbegin(),str.rend());
   cout<<str;
   return 0;
} 

结果:wroolllhde空格

6.关键字auto

auto可以在声明变量的时候根据变量初始值的类型自动为此变量选择匹配的类型,用于代替冗长复杂、变量使用范围专一的变量声明。

7.map遍历

对于一个map,以下两行的循环是错误的,map不能用i如下遍历:

   forint i=0; i<map.size();  i++) 
   TaskInfo* pInfo = map[i];

一种正确的遍历:

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->first下标的值,it->second所在下标的值
   return 0;
}

文件输入输出

(1)
Benchmark.txt内容如下:

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++代码:

#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;
}

运行结果:

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

猜你喜欢

转载自blog.csdn.net/qq_32732581/article/details/85037175