<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ##實戰c++中的vector系列--copy set to vector(別混淆了reserve和resize) stl算法中有個copy函數,我們可以輕松的寫出這樣的代碼: ~~~ #include <iostream> #include <algorithm> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double darray[10]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9}; vector<double> vdouble(10); vector<double>::iterator outputIterator=vdouble.begin(); copy(darray,darray+10,outputIterator); while(outputIterator!=vdouble.end()) { cout<<*outputIterator<<endl; outputIterator++; } getchar(); return 0; } ~~~ 于是你想使用copy來set to vector,于是你這樣寫道: ~~~ #include<iostream> #include<vector> #include<set> int main() { std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output; std::copy(input.begin(), input.end(), output.begin()); return 0; } ~~~ 編譯通過,運行的時候出現錯誤。? why? 方法一? 如果你在定義output的時候,指定其大小,錯誤就會消失: ~~~ #include<iostream> #include<vector> #include<set> int main() { std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output(2);//指明大小 std::copy(input.begin(), input.end(), output.begin()); std::cout << output.size() << std::endl; return 0; } ~~~ 方法二:使用**back_inserter**? back_inserter 是iterator適配器,它使得元素被插入到作為實參的某種容器的尾部 ~~~ #include<iostream> #include<vector> #include<set> #include<iterator> int main() { std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output; std::copy(input.begin(), input.end(), std::back_inserter(output)); std::cout << output.size() << std::endl; return 0; } ~~~ 再繼續vetor to vector: ~~~ #include<iostream> #include<vector> #include<set> #include<iterator> int main() { std::vector<std::string> v, orig; orig.push_back("first"); orig.push_back("second"); v = orig; v.insert(v.end(), v.begin(), v.end()); // Now v contains: { "first", "second", "", "" } v = orig; std::copy(v.begin(), v.end(), std::back_inserter(v)); v = orig; v.reserve(v.size() * 2); v.insert(v.end(), v.begin(), v.end()); // Now v contains: { "first", "second", "first", "second" } v = orig; v.reserve(v.size() * 2); std::copy(v.begin(), v.end(), std::back_inserter(v)); // Now v contains: { "first", "second", "first", "second" } // GOOD (best): v = orig; v.insert(v.end(), orig.begin(), orig.end()); // note: we use different vectors here // Now v contains: { "first", "second", "first", "second" } return 0; } ~~~ 再看這個:如果不resize,就會崩潰,把resize換成reserve也會崩潰,和解? ~~~ #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int myints[] = { 10, 20, 30, 40, 50, 60, 70 }; vector<int> myvector; vector<int>::iterator it; //myvector.resize(7); // 為容器myvector分配空間 ,不resize會崩潰 copy(myints, myints + 7, myvector.begin()); cout << "myvector contains: "; for (it = myvector.begin(); it != myvector.end(); ++it) { cout << " " << *it; } cout << endl; return 0; } ~~~ ================================================================ vector有reserve何resize,二者何為不同? vector 的reserve增加了vector的capacity,但是它的size沒有改變!而resize改變了vector的capacity同時也增加了它的size!? 原因如下:? reserve是容器預留空間,但在空間內不真正創建元素對象,所以在沒有添加新的對象之前,不能引用容器內的元素。加入新的元素時,要調用push_back()/insert()函數。 ~~~ resize是改變容器的大小,且在創建對象,因此,調用這個函數之后,就可以引用容器內的對象了,因此當加入新的元素時,用operator[]操作符,或者用迭代器來引用元素對象。此時再調用push_back()函數,是加在這個新的空間后面的。 兩個函數的參數形式也有區別的,reserve函數之后一個參數,即需要預留的容器的空間;resize函數可以有兩個參數,第一個參數是容器新的大小, 第二個參數是要加入容器中的新元素,如果這個參數被省略,那么就調用元素對象的默認構造函數。 ~~~ 看看實例: ~~~ #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> vector_for_reserve; vector<int> vector_for_resize; vector_for_reserve.reserve(4); vector_for_resize.resize(4); //size:0 capacity:4 cout << vector_for_reserve.size() << " " << vector_for_reserve.capacity() << endl; //size :4 capacity:4 cout << vector_for_resize.size() << " " << vector_for_resize.capacity() << endl; vector_for_reserve[0] = 1;//錯誤 vector_for_resize[0] = 1; return 0; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看