Dynamically allocated arrays or std::vector

前端 未结 10 1904
日久生厌
日久生厌 2021-01-12 21:46

I\'m trying to optimize my C++ code. I\'ve searched the internet on using dynamically allocated C++ arrays vs using std::vector and have generally seen a recommendation in f

10条回答
  •  悲&欢浪女
    2021-01-12 22:45

    One reason you're code might not be performing quite the same is because on your std::vector version, you are incrimenting two values, the integer i and the std::vector::iterator vecIt. To really be equivalent, you could refactor to

    start = clock();
    for (int i = 0; i < vec.size(); i++) {
      vec[i] = i;
    }
    end = clock();
    cout<<"vector: "<<(double)(end-start)/CLOCKS_PER_SEC<

提交回复
热议问题