0

I just wonder if it is possible to new and initialize a std::vector at the same time, something like, do the two things in one line:

std::vector<int>* vec = new std::vector<int>(){3, 4};

instead of, first:

std::vector<int>* vec = new std::vector<int>();

then:

vec->push_back(3);
vec->puch_back(4);
JeJo
  • 30,635
  • 6
  • 49
  • 88
Hu Xixi
  • 1,799
  • 2
  • 21
  • 29
  • 2
    Not only that, but there's no need to even use `new`. – StoryTeller - Unslander Monica Sep 24 '21 at 07:50
  • 1
    `new`ing a `std::vector` is suspicious BTW. Is `std::vector vec{3, 4};` sufficient? – Jarod42 Sep 24 '21 at 07:51
  • @StoryTeller - Unslander Monica Sure, but I need a vector that create on heap, so I need to new one. – Hu Xixi Sep 24 '21 at 07:52
  • 5
    The vector already manages its memory dynamically. There is virtually no reason to `new` the vector itself directly. – StoryTeller - Unslander Monica Sep 24 '21 at 07:54
  • 1
    @HuXixi _"but I need a vector that create on heap, so I need to new one"_ Where did you learn that? As mentioned `std::vector` already allocates memory dynamically. – πάντα ῥεῖ Sep 24 '21 at 07:58
  • 1
    @HuXixi If you really believe you need a pointer dynamically allocated, rather use a `std::unique_ptr>` or a `std::shared_ptr>` and decide ownership explicitly. – πάντα ῥεῖ Sep 24 '21 at 08:05
  • Most teachers start with teaching new/delete, instead of the use the standard library and (const) references. Rule of thumb : New and delete in C++ are not the first things to look at. Just use the stl types without pointers, pass them around by reference (or let the move constructors do the optimized work for you) – Pepijn Kramer Sep 24 '21 at 08:06

2 Answers2

4

I just wonder if is possible to new and initialize a std::vector at the same time, something like, do the two things in one line?

Yes, you can, via std::initializer_list constructor10 of std::vector

constexpr vector( std::initializer_list<T> init,
                  const Allocator& alloc = Allocator() ); (since C++20)

With you can write

std::vector<int>* vec = new std::vector<int>{3, 4};

Because I need a vector that create on heap!

The terms we use in C++ are automatic and dynamic storage. In most of the cases, you do not require the std::vector<int> to be allocated dynamically, rather the elements to be there. For this, you need simply a vector of integers.

std::vector<int> vec {3, 4};

However, if you're meant for a multidimensional vector, then I will suggest having a vector of vector of inters:

std::vector<std::vector<int>> vec{ {3, 4} };

When the inner vector has the same number of length, keep a single std::vector and manipulate the indexes for acting as a two-dimensional array.

In both cases, the std::vector in the background does the memory management for you.

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Thanks, and because I need a vector that create on heap. – Hu Xixi Sep 24 '21 at 07:53
  • 4
    @HuXixi vector manages memory for your. vector internally allocates on the heap. – Raildex Sep 24 '21 at 07:56
  • 1
    @HuXixi there is no concept of heap and stack in C++. Why do you need that anyway? – RoQuOTriX Sep 24 '21 at 07:58
  • @RoQuOTriX What if I want to put a std::vector<>* into a pthread_create as an argument? Then should I new a std::vector<>? – Hu Xixi Jan 17 '22 at 12:14
  • @HuXixi you can use pointers or references to pass the vector. Depending on the life cycle of the vector (where you start and join the thread and where the vector is created) It also depends if you have to "new" it (better use smart pointers) and then delete it in the new created thread. But this question is very context specific. Do you need specific help with memory management in threads? You could ask a new question with your problem (and link it here maybe, I would be interested in it :D ) – RoQuOTriX Jan 18 '22 at 13:30
2

This std::vector<int>() calls the default constructor, but there are other constructors: https://en.cppreference.com/w/cpp/container/vector/vector.

There is also a constructor that takes a std::initializer_list<T> that you can use: std::vector<int>({3,4}).

This is no different whether you use new to allocate the vector or not. However, there is almost never a good reason to allocate a std::vector via new, because the vector already does store its elements in dynamically allocated memory.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • What if I want to put a std::vector<>* into a pthread_create as an argument? – Hu Xixi Jan 17 '22 at 12:15
  • first you should be using `std::thread`, but if for whatever reason you are bound to pthreads, then ... I dont understand the question ;). You do not need to dynamically create an object to get a pointer to it. `std::vector vec; std::vector* vec_pointer = &vec;` – 463035818_is_not_an_ai Jan 17 '22 at 12:17
  • yes, ;) it is something that extend from the above question, but if I put a local vector's pointer into a pthread_create, that will cause segment fault, so I think I should new a vector on heap explicitly in this situation. – Hu Xixi Jan 18 '22 at 02:57