Create linked vector and lists

I want to create a vector that stores pointers to lists, as shown in the image. This is how it looks I donโ€™t know how many listings will be needed here. So, I want to write such a function

vector<node*> address; //node is class. if ((int)address.size()<number) //number is integer taken as input to function. { while((int)address.size()!=number) {address.push(/*here I want some function which will generate empty list and return pointer to head node or any information that will help to access list later*/) } else { address[number-1].push_back(name); //name has type string; //this line may be wrong for syntax but idea is A[i] gives me // list where I want to put name. } 

It is preferable to use the STL library.

+4
source share
2 answers

If you want to use the STL library just use

 std::vector<std::list<node>> address; //node is class (note that you can pass size here) // Using your code in your code: if ((int)address.size() < number) //number is integer taken as input to function. { address.resize(number); } else { address.back().push_back(name); //name has type string; } 

Note that node is the type of elements you want to insert into the vector. As @john said, if you want to keep a list of strings, declare address as:

  std::vector<std::list<std::string>> address; 

Also, if you get errors due to >> , either compile this as C ++ 11, or write address as:

  std::vector<std::list<std::string> > address; 
+4
source

This is not very clear, but I think you want a container with automatic resizing (like javascript vectors) where index lists are based on 1 (i.e. there is no list at address 0) and a method to insert at the end of one list on given index. Basically something like this:

 struct MyCollection: public std::vector<std::list<std::string> > { void push_at(size_t index, const std::string& item) { resize(std::max(size(),index - 1); at(index - 1).push_back(item); } }; 

Everything you want from such a container is most likely already implemented in the vector and list template classes (check the stl documentation to see what is available), for example:

 MyCollection a; //declares an empty collection a.push_at(6,"hello"); //create 6 lists and inserts "hello" at the end of the last one a[5]; //gets 6th list (method [] is 0-based on vector) a.push_at(6,"hi"); // since list already exists it just adds "hi" at the end a[5].front() //"hello" a[5].back() //"hi" 

Other offers:

  • if you plan to place a lot of elements there and should not have the whole list of objects at consecutive addresses (i.e. compatibility of a vector with C arrays), I suggest you use deque instead of vector , or provide a hint about the correct reserve size, otherwise you may be interested why sometimes adding one line to a list that does not yet exist is so slow.
  • STL uses 0-based indexes. You will only be confused by defining 1-based, so count your lists from 0 (just replace index - 1 with index in the above example) and do your math in the application logic)
+1
source

Source: https://habr.com/ru/post/1502247/


All Articles