Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a C++ string/vector with specified length but no initialization?

I need create a string/vector. I know how long it should be, however, I'd like to write the right thing into it later. Can I create it with a specified length but without any initialization (neither explicit nor implicit), like what malloc does? Because I'll write into it properly before reading from it, it would be a waste of time to initialize it at construction.

I hoped I could write with arbitrary order after creating the vector, like

vector<int> v(10); // Some magic to create v with 10 of uninitialized ints
v[6] = 1;
v[3] = 2;
...

Seemingly that's impossible.

like image 497
cedrusx Avatar asked May 19 '16 08:05

cedrusx


People also ask

How do you initialize a vector with a specific size?

To initialize a two-dimensional vector to be of a certain size, you can first initialize a one-dimensional vector and then use this to initialize the two-dimensional one: vector<int> v(5); vector<vector<int> > v2(8,v); or you can do it in one line: vector<vector<int> > v2(8, vector<int>(5));

Do vectors need to be initialized?

You will not be able to create a Vector, which is not initialized. The default constructor constructs an empty container with a default-constructed allocator. Here are some examples on how to initialise a Vector if you already know its content or size.

Can I make a vector of strings?

A vector of strings is created the way a vector of any other type would be created. Remember to make the template specialization, string. Do not forget to include the string library and the vector library. The common ways of creating vectors with string as the element type have been illustrated above.

How do you specify the size of a vector?

We can set the size of a Vector using setSize() method of Vector class. If new size is greater than the current size then all the elements after current size index have null values. If new size is less than current size then the elements after current size index have been deleted from the Vector.


2 Answers

If I understand your question properly, you want std::vector::reserve or std::basic_string::reserve.

std::vector<int> v;               // empty vector
v.reserve(how_long_it_should_be); // insure the capacity
v.push_back(the_right_thing);     // add elements
...

Edit for question's edit

vector<int> v(10);, will always construct v with 10 default-initialized int, i.e. 0. You might want std::array if you could know the size at compile time.

std::array<int, 10> v;  // construct v with 10 uninitialized int
v[6] = 1;
v[3] = 2;

LIVE

like image 165
songyuanyao Avatar answered Oct 30 '22 02:10

songyuanyao


Using .reserve() on either containers will increase the .capacity() of the internal memory block allocated without calling any default constructors.

You can assert that the container has the right capacity at the moment you need it using .capacity(). Note that .size() will be different to .capacity() after a .reserve() as the first returns the number of actual objects inside the container, while the seconds returns the total number of objects the current memory block can handle without reallocation.

It is good practice (especially for std::vector) to empirically .reserve() your containers to avoid extra allocations at runtime. If you are using at least C++11, in case you want the remaining memory back and you can deal with some copying/moving, you can use shrink_to_fit().

Note that std::string::reserve differs from std::vector::reserve in case the new capacity requested is smaller than the current capacity. The string will take it as a non-binding request to shrink, while the vector will ignore the request.

like image 36
george_ptr Avatar answered Oct 30 '22 03:10

george_ptr