-1

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
  • 26,381
  • 6
  • 42
  • 81
Hu Xixi
  • 1,551
  • 16
  • 25

2 Answers2

2

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
  • 26,381
  • 6
  • 42
  • 81
  • 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_a_number
  • 88,680
  • 9
  • 76
  • 150
  • 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_a_number 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