向vector、string添加元素後,若存儲空間被重新分配,則指向容器的迭代器、指針、引用都失效

// vector、string添加元素後,若存儲空間被重新分配,則指向容器的迭代器、指針、引用都失效。
// v.front()、v.back()返回的是元素的引用,類型和元素類型一樣
// v.begin()、v.end()返回的元素的位置,類型是迭代器,要得到元素的值需要解引用
// v.front()、v.back()和v.begin()、v.end()都能達到訪問首尾元素的效果,但它們不一樣

#include<iostream>
#include<vector>
#include<sstream>
#include<forward_list>

using namespace std;
int main() {
    vector<int> v = { 0,1,2,3,4,5};       // 容器v
    vector<int> &refv = v;          // v的引用
    vector<int> *p = &v;            // v的指針
    vector<int> *refp = &refv;      // 引用的指針
    auto v_front = v.front();        // v的首元素的引用
    auto v_begin = v.begin();       // 指向v的首元素的位置
    auto v_back = v.back();          // v的尾元素的引用
    auto v_end = v.end();           // 指向v的尾元素的下一個位置
    --v_end;
    cout << "修改前"<<endl<<"v:";
    for (auto it : v) {
        cout << it<<" ";
    }
    cout << endl << endl << "refv:";
    for (auto it : refv) {
        cout << it<<" ";
    }
    cout << endl << endl << "p:" << " " << p << endl;
    cout << endl << "refp:" << " " << refp << endl;
    cout << endl << "首元素的引用v_front:" << "  " << v_front << endl;
    cout << endl << "尾元素的引用v_back:" << "  " << v_back << endl;
    cout << endl << "*v_begin:" << "  " << *v_begin << endl;
    cout << endl << "*v_end:" << "  " << *(v_end) << endl;
    // 添加元素
    v.push_back(3456789);
    cout << endl << "修改後" <<endl<< "v:" << " ";
    for (auto it : v) {
        cout << it << " ";
    }
    cout << endl << endl << "refv:";
    for (auto it : refv) {
        cout << it << " ";
    }
    cout << endl << endl << "p:" << " " << p << endl;
    cout << endl << "refp:" << " " << refp << endl;
    cout << endl << "首元素的引用v_front:" << "  " << v_front << endl;
    cout << endl << "尾元素的引用v_back:" << "  " << v_back << endl;
    cout << endl << "*v_begin:" << "  " << *v_begin << endl;
    cout << endl << "*v_end:" << "  " << *(v_end) << endl;
    system("pause");
}

在向v中添加元素後,v.front()、v.back()返回的仍是v修改前的首尾元素,而迭代器v.begin()、v.end()失效,因此每次修改容器後都必須重新定位迭代器。
在這裏插入圖片描述

在這裏插入圖片描述在這裏插入圖片描述
向v中添加元素後,v的元素沒有失效,指針也沒有改變。也許是個例?
在這裏插入圖片描述
在修改v後,重新定位迭代器,結果正確

#include<iostream>
#include<vector>
#include<sstream>
#include<forward_list>

using namespace std;
int main() {
    vector<int> v = { 0,1,2,3,4,5};       // 容器v
    vector<int> &refv = v;          // v的引用
    vector<int> *p = &v;            // v的指針
    vector<int> *refp = &refv;      // 引用的指針
    auto v_front = v.front();        // v的首元素的引用
    auto v_begin = v.begin();       // 指向v的首元素的位置
    auto v_back = v.back();          // v的尾元素的引用
    auto v_end = v.end();           // 指向v的尾元素的下一個位置
    --v_end;
    cout << "修改前"<<endl<<"v:";
    for (auto it : v) {
        cout << it<<" ";
    }
    cout << endl << endl << "refv:";
    for (auto it : refv) {
        cout << it<<" ";
    }
    cout << endl << endl << "p:" << " " << p << endl;
    cout << endl << "refp:" << " " << refp << endl;
    cout << endl << "首元素的引用v_front:" << "  " << v_front << endl;
    cout << endl << "尾元素的引用v_back:" << "  " << v_back << endl;
    cout << endl << "*v_begin:" << "  " << *v_begin << endl;
    cout << endl << "*v_end:" << "  " << *(v_end) << endl;
    // 添加元素
    v.push_back(3456789);
    // 重新定位迭代器
    v_front = v.front();        // v的首元素的引用
    v_begin = v.begin();       // 指向v的首元素的位置
    v_back = v.back();          // v的尾元素的引用
    v_end = v.end();           // 指向v的尾元素的下一個位置
    --v_end;
    cout << endl << "修改後" <<endl<< "v:" << " ";
    for (auto it : v) {
        cout << it << " ";
    }
    cout << endl << endl << "refv:";
    for (auto it : refv) {
        cout << it << " ";
    }
    cout << endl << endl << "p:" << " " << p << endl;
    cout << endl << "refp:" << " " << refp << endl;
    cout << endl << "首元素的引用v_front:" << "  " << v_front << endl;
    cout << endl << "尾元素的引用v_back:" << "  " << v_back << endl;
    cout << endl << "*v_begin:" << "  " << *v_begin << endl;
    cout << endl << "*v_end:" << "  " << *(v_end) << endl;
    system("pause");
}

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章