簡體   English   中英

Windows Visual Studio C++ 列表列表

[英]Windows Visual Studio C++ lists of lists

Windows 列表 發現以下帖子有些幫助,但我仍在苦苦掙扎。

在 c++ 中創建列表列表

我需要的是一個字符串列表。

所以:

#include <list>

typedef list<std::string, allocator<std::string>>> LISTSTR;
typedef list<LISTSTR, allocator<LISTSTR>> LISTLISTSTR;  // uncertain what this is doing?!!?

main()
{
  LISTLISTSTR records;

  // Add a blank string list to the list

  LISTSTR *rowoffields = new LISTSTR();

  rowoffields->insert(string("Field1"); // also tried new string("Field1")
  records.insert(rowoffields);  // also tried *rowoffields
}

但這會返回編譯錯誤:

no instance of overloaded function "std::list<_Ty,_Alloc>::insert[with _Ty=std::string, _Alloc=std::allocator<std:string>]" matches the argument list
argument types are (std::string)  // or types are (std::string*) if using new string("Test")
object type is: LISTSTR
no instance of overloaded function "std::list<_Ty,_Alloc>::insert[with _Ty=LISTSTR, _Alloc=std::allocator<LISTSTR>]" matches the argument list
argument types are (LISTSTR*)  // or types are (LISTSTR) if using *rowoffields
object type is: LISTLISTSTR

這是 go 的一種方法,它可能更接近 Python 的正常用法。 我們首先聲明一個指向字符串列表的指針數組(預先指定類型,而不是 Python-duck 類型(盡管我們可以使用模板來做到這一點)。然后我們以內存安全的方式使用這些指針(不泄漏)來創建新列表。請注意,在此特定設置中,列表的總數在編譯時是固定的(並且等於 const int SIZE),並且指針數組位於堆棧上,但列表指針指向to 都是在堆上動態分配的。(你可以用 new 和 delete 來做到這一點,但更新的智能指針確實是要走的路)。

#include <list>         
#include <memory>       //for the smart pointers
#include <iostream>    

const int SIZE = 3;

int main() {
    //make an array of smart pointers to list of strings:
    std::unique_ptr<std::list<std::string>> myLists[SIZE];
    //use each smart pointer in the list to make a new list:
    for (int i = 0; i < SIZE; i++) {
        myLists[i] = std::make_unique<std::list<std::string>>();
    }
    //load each new list with a single string:
    myLists[0]->push_front("Hello");
    myLists[1]->push_front("World,");
    myLists[2]->push_front("I'm C++!");
    //print the results
    for (int i = 0; i < SIZE; i++) {
        std::cout << myLists[i]->front() << " ";
    }
    std::cout << std::endl;
    return 0;
}

現在,如果您希望能夠在運行時向列表列表添加更多列表,則必須動態分配指向列表的指針數組,而不是像本例中那樣使用 static(自動)分配。 (編輯:或者,您可以設置一些上限,例如 100,然后將其設為應用程序中列表的最大數量。可能想要使用 std::array 來保存該指針列表,而不是上面的普通數組,因為 std::array 會像使用 Python 一樣進行邊界檢查)。 對於更簡單的版本:

#include <list>
//remove typedefs
int main() {      //notice the int!
    const int SIZE = 10;
    //make array of pointers to list of strings (on stack, fixed size)
    std::list<std::string>* myLists[SIZE];
    //use pointer to make new list on heap:
    myLists[0] = new std::list<std::string>;
    //use pointer syntax (instead of . use ->) to call list functions
    myLists[0]->push_front("Old style C++");
    //more code...
    //when finished must remember to free that memory:
    delete myLists[0];
    //and not leave a dangling pointer:
    myLists[0] = nullptr;
    return 0;
}

暫無
暫無

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

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