How to create a vector of user defined size but with no predefined values? - c++

In C++ one can create an array of predefined size, such as 20, with int myarray[20]. However, the online documentation on vectors doesn't show an alike way of initialising vectors: Instead, a vector should be initialised with, for example, std::vector<int> myvector (4, 100);. This gives a vector of size 4 with all elements being the value 100.
How can a vector be initialised with only a predefined size and no predefined value, like with arrays?

With the constructor:
// create a vector with 20 integer elements
std::vector<int> arr(20);
for(int x = 0; x < 20; ++x)
arr[x] = x;

Related

How to assign a specific number to all elements of matrix in vector<vector<int>> matrix_name without using for loop stuff?

like we do this in array thing to assign a specific num to all elements of array-
vector<int> arr(n,-1);
So ya,above i mention how to do it in array but what about vector<vector> matrix_name?
Just as you can initialize a vector with a number of integers, given a default integer value, you can also initialize a vector with a number of other vectors, given a default value for that other vector. That way you can easily create a multidimensional vector.
Example:
int nr_of_rows = 30;
int nr_of_columns = 40;
std::vector matrix(nr_of_rows , std::vector(nr_of_columns , -1));
This creates a vector of 30 vectors of 40 integers with a default value of -1.
matrix[2][17] gives you vector with index 17 inside vector with index 2 of your matrix.

How to get constant size of vector for an array declaration?

I am reading the data from text file which is basically has rows and columns of double data types.
Part of the code is as shown below:
m_data = vector<vector<double> >(columns, vector<double>(lineCount - 3));
for (int x = 0; x < lineCount - 3; x++)
{
for (int y = 0; y < columns; y++)
{
m_data[y][x] = total_Data[it]; //total_Data is the complete data set read from a file
it++; //which contain all data sets from a simulation and they are
//separated into vectors here in this code
}
}
size_t len = m_data[1].size(); //m_data[1] vector represents the signal data set
double signaldata[len];
copy(m_data[1].begin,m_data[1].end,signaldata);
So the problem is when I copy the data from vector to array it requires the constant length of the array for initialization. But size() gives the length of the vector which is not a constant.
And sizeof(vector) returns the size of the object itself which is 16 bytes.
So how I can overcome this problem of getting a constant size from vector for array initialization
The answer is: You can't.
An std::vector is designed to have dynamic size, that is its size can change at runtime (i.e by loop from user input). In C++ it is not legal to create an 'array' of a non-compile time constant size. You can however create a dynamically allocated 'array' with new (e.g auto my_array = new char[vector.size()]). You really should not do this however, especially without doing any sanity checks on the size of the vector.
An alternative would be to use std::vector directly. If you have to work with an older (or C) API which requires a pointer to the beginning of your 'array' as an argument you can use vector::<T>::data which will return &vector[0].
So if I have this code:
void my_old_api_function(char* c) {
...
}
I can do this:
std::vector<char> my_data;
my_data.emplace_back('c');
...
my_old_api_function(my_data.data()); // Pass a pointer to the first element

Resize inner dimension of 2D vector

I want to create a vector with inner dimension having a initialize size of 10 and outer dimension can have any size which grows dynamically:
std::vector<std::vector<int>>;
The inner must have an initial size of 10. Is this possible ?
It is possible to initialize the size of the inner vectors to 10:
std::vector<std::vector<int>> foo(n, // will contain n inner vectors
std::vector<int>(10)); // each inner vector has size 10 initially
When you use vector of vectors, the inner vector is also dynamic in nature. You just declare and initialize it when defining the inner vector, then push it to outer vector.
const int col_size = 10;
vector<vector<int> > table;
vector<int> row(col_size, 0);
table.push_back(row);
Code below defines an array of size n*m and filled with 0: (noting that this does not restrict the dimension of vector appended in future)
const int m = 5, n = 4;
vector<vector<int> > arr(n, vector<int>(m,0));
An alternative approach is to define your own structure for inner dimension using static type:
const int col_size = 3;
struct Row {
int val[col_size];
// to behave like an array.
int operator[](int x) { return val[x]; }
};
vector<Row> table;
As others here, mentioned that while using vectors you probably dont have to worry about allocation/de-allocation, std::vector can grow their sizes if required. if you want to use fixed (some what fixed, which which change if some new elements are pushed) size vector you can use can reserve slots by using std::vector::reserve

How to give a size to a vector array?

Hello I would like to set 18 elements in my vector array but i'm unsure on how to do it exactly. Would it be like this?
vector<byte> Clients(18);
or like this?
vector<byte> Clients[18];
Vectors are not called vector arrays. They are usually implemented as arrays in the background, but that doesn't change what they are called.
To answer your question, to change the size of a vector you can use the resize member function.
std::vector<int> myvector;
myvector.resize(5);
You can also use resize to initialize all the values. For instance, in the following example there are five 0's in the vector.
std::vector<int> myvector;
myvector.resize(5, 0);
Usually, you just push things into a vector and don't have to set a size. For example:
std::vector<int> myvector;
for (int i = 0; i < 5; i++) {
myvector.push_back(
}
// you now have a vector with a size of 5 that has initialized
// values at indices 0 through 4, inclusive.

assigning vector

how I can assign v[i] to an series of integers ( type of v is vector ) without initially filling inside
Do you mean initializing std::vector to a series of integers?
int i[] = {1, 2, 3, 4, 5};
std::vector<int> myVector(i, i+ (sizeof(i)/sizeof(int)));
If you meant to create a vector of some elements so you can perform the assignment using their index values. Here, the following statement declares and initializes a vector with its elements being default initialized to 0.
std::vector<int> myVector(5); // constructs a vector of size five integers.
for (int x = 0; x < 5; x++)
myVector[x] = i[x]; // assign values using subscript [..]
But I think the even better way to go would be as #CashCow mentioned in his answer.
Also, note that you can also pre-allocate memory to add elements into the vector with avoiding any repeated memory allocations.
For example:
std::vector<int> myVector; // empty vector for integers
myVector.reserve(5); // pre-allocates memory for five integers
for (int i = 0; i < 5; i++) // now, you can add your elements
myVector.push_back(i);
It is usually a good idea to pre-allocate memory if you know the size of elements i.e in case of large number of elements when the performance is an important factor.
If you have anything that has the traits of an iterator you can use vector's assign method:
std::vector<int> v;
v.assign( iterStart, iterEnd );
iterStart should be such that *iterStart is the first value you want to add.
iterEnd should be one past the end, it is a terminating condition
++iter would move you to the next iterator in the input series.
I don't know what you mean by assign v[i] though. You cannot assign an element to a series. If you want to write the series at a location into an existing vector you can use insert instead of assign.
common way of adding items is calling std::vector<>push_back() method.
std::vector<int> myVector;
myVector.push_back(5);
myVector.push_back(10);
myVector.push_back(3);