↓ 跳过正文
  1. Posts/

把二进制文件按字节读到vector中

·197 字·1 分钟
 1    std::vector<unsigned char> readFromFile1(const char* filePath) {
 2        FILE* file = fopen(filePath, "rb");
 3        std::vector<unsigned char> result;
 4        if (file == nullptr) {
 5            return result;
 6        }
 7
 8        // 获取文件大小,尽量一次读完
 9        size_t fileSize = getFileSize(file);
10        if (fileSize != 0) {
11            result.resize(fileSize);
12            size_t n = fread(&result[0], 1, fileSize, file);
13            assert(n <= fileSize);
14            if (n != fileSize) {
15                result.resize(n);
16            }
17        }
18
19        // 在读取过程当中,有可能文件大小有变化,再尝试读取
20        const size_t read_len = 1024;
21        char buf[read_len];
22        for (;;) {
23            size_t n = fread(buf, 1, read_len, file);
24            result.insert(result.end(), buf, buf + n);
25            if (n < read_len) {
26                break;
27            }
28        }
29        fclose(file);
30        return result;
31    }

另外一种C++风格,但是性能较差的方法:

1
2    std::vector<unsigned char> readFromFile2(const char* filePath) {
3        std::ifstream inputFile(filePath, std::ios::binary);
4        std::vector<unsigned char> fileData((std::istreambuf_iterator<char>(inputFile)),
5                                            std::istreambuf_iterator<char>());
6        return fileData;
7    }

相关文章

boost:property_tree 学习笔记

·254 字·1 分钟
先放资料: How to use boost::property_tree to load and write JSON How to iterate a boost property tree? 不出现key的方法遍历一个json文件: 详细代码 1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年08月17日 星期五 15时29分23秒 5 File Name :ptree.cpp 6 ************************************************ */ 7 #include <boost/property_tree/ptree.hpp> 8 #include <boost/property_tree/json_parser.hpp> 9 #include <bits/stdc++.h> 10 #include <vector> 11 using namespace std; 12 using boost::property_tree::ptree; 13 14 string indent(int level) { 15 string s; 16 for (int i=0; i<level; i++) s += " "; 17 return s; 18 } 19 20 void printTree (ptree &pt, int level) { 21 if (pt.empty()) { 22 cerr << "\""<< pt.data()<< "\""; 23 } 24 25 else { 26 if (level) cerr << endl; 27 28 cerr << indent(level) << "{" << endl; 29 30 for (ptree::iterator pos = pt.begin(); pos != pt.end();) { 31 cerr << indent(level+1) << "\"" << pos->first << "\": "; 32 33 printTree(pos->second, level + 1); 34 ++pos; 35 if (pos != pt.end()) { 36 cerr << ","; 37 } 38 cerr << endl; 39 } 40 41 cerr << indent(level) << " }"; 42 } 43 44 return; 45 } 46 int main() 47 { 48 ptree root; 49 read_json("terr.json",root); 50 printTree(root,0); 51 52 53 return 0; 54 55 }

c++11 function 与bind 学习笔记

·315 字·1 分钟
C++11 std::function 是一种通用、多态的函数封装,它的实例可以对任何可 以调用的目标实体进行存储、复制和调用操作 见下面的例子 1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年07月19日 星期四 17时41分00秒 5 File Name :bind.cpp 6 ************************************************ */ 7 #include <functional> 8 #include <iostream> 9 using namespace std; 10 float foo( int x,int y,int z){return x+y+z+1.;} 11 int main() 12 { 13 function<int(int,int)>func = foo; 14 int y = 10; 15 function<int(int)>fun = [&]( int value)->int 16 { 17 return 1+value+y; 18 }; 19 cout<<func(15,4,9)<<endl; 20 cout<<fun(8)<<endl; 21 return 0; 22 } std::bind 则是用来绑定函数调用的参数的,它解决的需求是我们有时候可 能并不一定能够一次性获得调用某个函数的全部参数,通过这个函数,我们可以将 部分调用参数提前绑定到函数身上成为一个新的对象,然后在参数齐全后,完成调 用

C++ 记录代码运行时间

·169 字·1 分钟
以前用的办法太老土啦 看到一个since C++11的方法,我觉得比较优雅 1 #include <iostream> 2 #include <chrono> 3 //#include <ratio> 4 #include <thread> 5 6 void f() 7 { 8 std::this_thread::sleep_for(std::chrono::seconds(1)); 9 } 10 11 int main() 12 { 13 auto t1 = std::chrono::high_resolution_clock::now(); 14 f(); 15 auto t2 = std::chrono::high_resolution_clock::now(); 16 17 // floating-point duration: no duration_cast needed 18 std::chrono::duration<double, std::milli> fp_ms = t2 - t1; 19 20 // integral duration: requires duration_cast 21 auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); 22 23 // converting integral duration to integral duration of shorter divisible time unit: 24 // no duration_cast needed 25 std::chrono::duration<long, std::micro> int_usec = int_ms; 26 27 std::cout << "f() took " << fp_ms.count() << " ms, " 28 << "or " << int_ms.count() << " whole milliseconds " 29 << "(which is " << int_usec.count() << " whole microseconds)" << std::endl; 30 }

gdb学习笔记

·235 字·1 分钟
用gdb调试c++的时候,需要添加-g编译选项add_compile_options(-g),并且关掉各种编译优化 如果是多线程程序,可以用info threads 查看每个线程的信息