64,682
社区成员
发帖
与我相关
我的任务
分享
//for_each bind
std::sort(line1.begin(),line1.end(),bind(cmp_radian_xAxis_forward_direction,P1,_1,_2));
for_each(node1.begin(),node1.end(),bind(&vector<CBLF_Line*>::push_back,line1,_1));
//shared_ptr
typedef vector<CBLF_Line*> BFSVector;// broad-first search queue of some connected graph
typedef shared_ptr<BFSVector> BFSVectorPtr;
//unordered_map
typedef unsigned long long ULL;
typedef unordered_set<CBLF_Line*> graph_node;
typedef unordered_map<ULL,graph_node> graph;
typedef unordered_set<CBLF_Line*> BLF_LineSet;
A::iterator begin = A.begin();
A::iterator end = A.end();
for(it = begin; it != end; ++it){...}
A::iterator end = A.end();
for(A::iterator it = A.begin(); it != end; ++it){...}
在c++11中for_each大部分时候也可以用range based for loop取代
for(auto const &data : input){
...}
>搞C++真要非常熟悉STL才行
这点我赞同
//比较简洁,速度可能比较慢
void CPP_SOLUTION_ONE()
{
std::ifstream inFile("before.txt");
std::vector<double> data( ( std::istream_iterator<double>(inFile) ), std::istream_iterator<double>() );
std::sort(std::begin(data), std::end(data) );
std::ofstream outFile("after.txt");
std::copy(std::begin(data), std::end(data), std::ostream_iterator<double>(outFile, "\n") );
}
//比较烦杂,速度可能比较快
void CPP_SOLUTION_TWO()
{
FILE *fin = fopen("before.txt", "r");
std::vector<double> data;
double number = 0;
while( fscanf(fin, "%lf", &number) == 1 ) data.push_back(number);
fclose(fin);
std::sort(std::begin(data), std::end(data) );
std::ofstream outFile("after.txt");
std::copy(std::begin(data), std::end(data), std::ostream_iterator<double>(outFile, "\n") );
}
纯C
#include<stdlib.h>
#include<stdio.h>
int compare(void const *p, void const *q)
{
double p0 = *(double*)p;
double q0 = *(double*)q;
if(p0 > q0) return 1;
else if(p0 < q0) return -1;
return 0;
}
void quit()
{
fprintf(stderr, "memory exhausted\n");
exit(1);
}
void C_SOLUTION()
{
int res = 1000; //initial allocation
double *buf = (double*)malloc(sizeof(double) * res);
if(buf == 0) quit();
int n = 0; //number of elements
int i = 0;
FILE *fin = fopen("before.txt", "rt");
double d;
while( fscanf(fin, "%lf", &d) == 1 )
{
if(n == res)
{
res += res;
buf = (double*)realloc(buf, sizeof(double) * res );
if(buf == 0) quit();
}
buf[n++] = d;
}
fclose(fin);
qsort(buf, n, sizeof(double), compare);
FILE *fout = fopen("after.txt", "wt");
for(i = 0; i != n; ++i) fprintf(fout, "%f\n", buf[i]);
fclose(fout);
free(buf);
}
对于熟悉stl语法的人而已,那个比较容易阅读?
vector<shared_ptr<string>> weird_codes;
除非你要share那个string,否则这种情况根本没有使用shared_ptr的必要
shared_ptr没必要的话就少用点,多用unique_ptr(没的话可考虑auto_ptr)
smart pointer的精神是RAII,但不见得所有的资源都要交给smart pointer管理
这也是RAII
vector<myObject> A;
STL确实有可能产生内存碎片,但是直接用手工分配记忆体
难道就不会造成内存碎片吗?这不是stl才有的问题
只要你频繁的分配更大的记忆体,释放掉旧的记忆体
fragmentation就无法避免,我们只能想办法缓和他的冲击
如果记忆体需要频繁的分配和释放,可以自己设计一个pool
只是单纯的malloc,free,new,delete
其实结果也跟使用stl没什么两样,搞不好还更糟
如果因为这个理由就完全不使用设计水平远在
一般水准之上的标准库,无异于因噎废食
always worry about performance and efficiency when your profiler tells you
class someClass{
public :
someClass()
{
a = new someObject;
//throw exception
b= new someObject;
}
~someClass() { delete a; delete b;}
private:
someObject *a;
someObject *b;
};
std::mutex lk;
void mfunction(){
lk.lock()
//do something and throw exception
lk.unlock(); //永远不会被解锁
}