读取配置文件
#include <iostream> #include <fstream> #include <sstream> #include <cstdlib> #include <string> using namespace std;
int main() {
char buf[1024]; ifstream fin("../default.cfg"); if (!fin.is_open()) { cerr << "open config file error\n"; exit(1); }
while (fin.getline(buf, 256)) { if (buf[0] == '#') // drop the comment line
continue; stringstream ss; ss << buf; string key, equal_token, value; ss >> key >> equal_token >> value; cout << "key = " << key << endl << "equal_token = " << equal_token << endl << "value = " << value << endl; }
fin.close(); return 0; }
|
配置文件:
# drving style DRIVING_STYLE = RA
|
对于更复杂的配置文件读取可用libconfig库
错误时输出当前文件,行号和函数
#include <iostream> using namespace std;
#define WARNING(msg) {cout << __FILE__ << " line" << __LINE__ \ << " " << __PRETTY_FUNCTION__ \ << ":" << msg << endl;} int main() { WARNING("connection failed"); return 0; }
|
数组初始化
int a[] = {1,2}; // ok
int *a = {1, 2}; // error
|
判断浮点数是否相等
#include <limits> #include <cmath> bool doubleCmp(double a, double b) { return abs(a - b) < numeric_limits<double>::epsilon(); }
|
heap改变<操作符定义前插入的元素大小顺序并不会因此改变
1,2范数计算
#include <limits> #include <iostream> #include <cmath> #include <numeric> using namespace std;
int norm1_add(double x, double y) { return abs(x) + abs(y); } int norm2_add(double x, double y) { return x + y * y; }
int main() { double a[] = {1, -2, 3}; cout << accumulate(a, a + 3, 0, norm1_add) << endl; cout << accumulate(a, a + 3, 0, norm2_add) << endl; return 0; }
|
阅读(997) | 评论(0) | 转发(0) |