簡體   English   中英

C ++如何使用指向矢量指針的指針

[英]C++ how to work with pointers to vector pointers

我有這個設計:

vector<string*>* tmp = new vector<string*>;

如何將元素放入其中? 我不明白如何將這個向量放入幾個字符串。 我嘗試過這個

std::string srt = "abc";
tmp->push_back(srt);

但是編譯器詛咒並且語法不正確,我不知道該怎么辦

只是不要變得過於指針式(假設這是一個相對簡單的程序)

vector<string> tmp;
std::string srt = "abc";
tmp.push_back(srt);

當然,指向字符串的指針的向量是一場等待發生的災難。 而且可能您也不需要指向向量的指針

語法為tmp->push_back(&srt);

但是最好使用vector<string>因為vector將為您處理指針。

首選:

std::vector<std::string> tmp;
std::string srt{"abc"};
tmp.push_back(srt);

要回答您的問題:

如何使用指向矢量指針的指針?

讓我們看一個基本的例子...

#include <iostream>
#include <string>
#include <vector>

int main() {
    // has to be initialized since it is a pointer.
    // However, this will compile but will fail at runtime.
    std::vector<std::string*>* pvWords = nullptr;

    std::string word1 = "Hello";
    std::string word2 = "World";

    // Here you need the `operator->()` from the vector
    // and since it will store pointers to strings,
    // you can just pass in the address of...
    pvWords->push_back( &word1 ); 
    pvWords->push_back( &word2 );

    for ( auto& s : *pvWords )    // here we need to dereference pvWords
        std::cout << *s << ' ';   // and here we need to dereference s
    std::cout << '\n';

    return 0;
}

-預期的輸出-

Hello World

但是,這將編譯但在運行時失敗,並在Visual Studio中導致訪問沖突錯誤。 之所以失敗,是因為一旦strings指針超出范圍,它們就會被銷毀,並且vectorvector*試圖引用的內存現在無效。 您也許可以通過使用動態內存來克服此問題,但是即使那樣,它也只會使代碼變得更加復雜且難以管理,並可能導致更多看不見的錯誤。

如果我們將以上內容修復為以下代碼段,則代碼將起作用:

#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string*>* pvWords = new std::vector<std::string*>;

    std::string word1 = "Hello";
    std::string word2 = "World";

    pvWords->push_back( &word1 ); 
    pvWords->push_back( &word2 );

    for ( auto& s : (*pvWords) )
        std::cout << (*s) << ' ';
    std::cout << '\n';

    delete pvWords; // don't forget to cleanup dynamic memory.

    return 0;
}

這將與動態內存一起使用,並將提供以下輸出:

Hello World

但是,為避免所有這些錯誤和代碼復雜性,只需不使用任何指針,尤其是在使用std::string



第一個解決方案可以工作,但是如果任何指針在使用之前變得無效,並且會避免運行時錯誤,則很棘手。

在這種情況下,無需使用指針。 您不會在需要繼承或多態性的地方存儲基類的派生類。

由於std::vectorstd::string的行為,只需這樣做:

#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> vWords;

    vWords.push_back( "Hello" );
    vWords.push_back( "World" );

    for ( auto& s : vWords )
        std::cout << s << ' ';
    std::cout << '\n';

    return 0;
}

由於std::vectorstd::string管理它們自己的內存,因此將產生相同的預期結果,而不會進行任何動態內存分配和內存清理,沒有任何錯誤,也沒有任何代碼復雜性。

-輸出-

Hello World

此版本的代碼更容易閱讀,更易於使用和調試,更易於其他人使用,並且將完全按照您的期望執行。 此解決方案非常簡單,快速且高效,無所有麻煩。

在沒有令人信服的理由的情況下,可以但不應該以一種方式使用向量,但我無法想到一個。

vector<string*>* tmp = new vector<string*>;

這是您必須執行的操作:

using std::vector; using std::string; using std::cout;

int main()
{
    vector<string*>* tmp = new vector<string*>;
    string srt = "abc";
    (*tmp).push_back(&srt);
    cout << *(*tmp)[0] << endl;
    delete tmp;
}

或者,可以使用間接運算符->代替* 他們做同樣的事情,但是在不同的地方看起來都很丑。

    tmp->push_back(&srt);
    cout << *tmp->operator[](0) << endl;

讓我們檢查*(*tmp)[0]發生了什么。 (*tmp)是由向量指向的tmp (*tmp)[0]是向量中第一個元素的內容,它是指向字符串的指針。 *(*tmp)[0]是向量中第一個元素指向的字符串。

除了有些晦澀的語法外,還要提防向量中超出范圍的元素所指向的字符串,這些字符串會創建懸掛的指針。

這是使用容器存儲字符串的規范方法。

int main()
{
    vector<string> tmp;
    string srt = "abc";
    tmp.push_back(srt);
    cout << tmp[0] << endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM