How to insert elements in it ?
vector <int> arr[10];
then how to insert more elements in the vector at position arr[i] when required?
#include <iostream>
using namespace std;
int main()
{
vector<int> v[3]; // here statically i have mentioned size,
// create 3 contiguous vectors of type int
//remember that it's is 2d kind
v[0].push_back(2);
v[0].push_back(3);
v[1].push_back(3);
// This is how you can add more elements than the
// specified size-dynamically can change the size
// This is the advantage of vector as we can
// dynamically add more elements
cout<<v[0].front(); //2
cout<<v[0].back(); //3
cout<<v[1].at(0) ; //3
//it's structure is something like this{{2,3},{3},{\0}}
return 0;
}
Related
I am new to C++ concepts,I am very much confused in inserting a value in a nested container.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<vector<vector<vector<int>>>>>s;
s[0][0][0][0].push_back(5);
return 0;
}
My program gets terminated abnormally.
You've default initialised s. The default constructor of vector creates an empty vector.
s[0] accesses the first element of the vector. An empty vector has no elements. This is a contradiction. The behaviour of accessing elements outside the bounds of the vector is undefined.
You could initialise a non-empty vector for example like this:
std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>> s
{ // std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>>
{ // std::vector<std::vector<std::vector<std::vector<int>>>>
{ // std::vector<std::vector<std::vector<int>>>
{ // std::vector<std::vector<int>>
{5}, // std::vector<int>
},
},
},
};
You have to insert elements to each vectors before accesing. Otherwise, you will wrongly access to a nonexistent element.
#include <vector>
using std::vector;
int main()
{
vector<vector<vector<vector<vector<int>>>>>s;
s.push_back(vector<vector<vector<vector<int>>>>());
s[0].push_back(vector<vector<vector<int>>>());
s[0][0].push_back(vector<vector<int>>());
s[0][0][0].push_back(vector<int>());
s[0][0][0][0].push_back(5);
return 0;
}
Either stick with vector and follow eerorika's suggestion or choose a container default-constructing elements upon access, like std::map:
#include <map> // don't use stdc++.h or using namespace std
int main()
{
template<class T>
using map = std::map<std::size_t, T>;
map<map<map<map<map<int>>>>> s;
s[0][0][0][0][0] = 5;
}
#include <iostream>
#include <array>
using namespace std;
int main(){
int a = [];
int b = 10;
std::fill(a);
cout<<a<<endl;
}
I have an array "a" and want to fill it with an integer "b". As I remember in python its simply uses apppend, does someone know solution?
Here one solution how to use your array header.
int b = 10;
std::array<int, 3> a;
std::fill(begin(a), end(a), b);
I have an array "a"
int a = [];
What you have is a syntax error.
As I remember in python its simply uses apppend
A major difference between a C++ array and python list is that the size of C++ array cannot change, and thus nothing can be appended into it.
How to fill array in C++?
There is indeed a standard algorithm for this purpose:
int a[4];
int b = 10;
std::fill_n(a, std::size(a), b);
Decide the size for a, as it is an array not a list. For example:
int a[10];
Then use index values to fill in the array like:
a[0] = 1;
a[1] = 4;
etc.
If you want a dynamic array use std::vector instead
Here is how its done with vectors:
std::vector<int> myvector;
int myint = 3;
myvector.push_back (myint);
Following zohaib's answer:
If your array is of fixed length:
You can use the array's 'fill' member function like this:
a.fill(b);
If your array can change it's size you can use the std's fill function like this:
std::fill(a.begin(), a.end(), b);
I am getting segmentation error in the following code, can anyone bother to explain. I think it might have to do with initialization, but not sure. I am just trying to clone the existing stack and perform operation such as adding entry to the clone or removing entry from existing and cloning it to new stack.
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <string>
using namespace std;
#define in cin
#define out cout
int main()
{
//ifstream in("postfix.in");
//ofstream out("postfix.out");
int n;
in>>n;
long sum=0;
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
//ar[0].push_back(0);
out<<ar[0][0];
for(int i=0;i<n;i++)
{
int ind,val;
in>>ind>>val;
if(val==0)
{
for(int j=0;j<ar[ind-1].size();j++)
ar[i].push_back(ar[ind-1][j]);
ar[i].pop_back();
}
else
{
for(int j=0;j<ar[ind-1].size();j++)
ar[i].push_back(ar[ind-1][j]);
ar[i].push_back(val);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<ar[i].size();j++)
sum+=ar[i][j];
}
out<<sum<<endl;
return 0;
}
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
Here you initialized ar as an empty vector of vector of int. Without enlarging its size through push_back(), resize(), insert(), etc., you cannot access ar[i].
You may instead initialize ar as
vector<vector<int>> ar(n);
But in the existing snippet you provided there is no clue about how large the second dimension should be.
Per your comment in this answer, your declaration of tm and ar should be
vector<int> tm(1, 0);
vector<vector<int>> ar(1, tm);
Or even shorter since tm is not really used later,
vector<vector<int>> ar(1, vector<int>(1, 0));
I think I see where you're getting tripped up.
vector<int> tm(0);
This does not create a vector containing a 0. This creates a vector with a size of 0. Because this list has a size of 0, you can't get the first element; since it's empty!
Same here:
vector<vector<int>> ar(0,tm);
This doesn't create a vector with 1 "row". It creates an empty vector, since you made the size 0 again.
You likely intended something like:
vector<int> tm(1, 0);
vector<vector<int>> ar(1,tm);
Which creates a row, tm, with a single 0, then creates a 2D vector, ar containing that 1 row.
Check the reference. You're attempting to use the "fill" constructor.
For starters it is a bad idea to use such definitions
#define in cin
#define out cout
It is better to use explicitly std::cin and std::cout because any programmer knows what these names mean.
These declarations
vector<int> tm(0);
vector<vector<int>> ar(0,tm);
do not make great sense. It would be much clear and simpler just to write
vector<int> tm;
vector<vector<int>> ar;
SO as the vectors are empty you may not use the subscript operator as you are doing for instance here
out<<ar[0][0];
^^^^^^^^^
for(int i=0;i<n;i++)
{
int ind,val;
in>>ind>>val;
if(val==0)
{
for(int j=0;j<ar[ind-1].size();j++)
^^^^^^^^^^^
ar[i].push_back(ar[ind-1][j]);
^^^^^
ar[i].pop_back();
^^^^^
}
and so on. You need at first ro append new elements to the vector before using the subscript operator.
Fruits class:
#include <string>
using namespace std;
class Fruits {
string color;
public:
Fruits() {
color = "r";
}
};
Main:
#include "Fruits.cpp"
void main() {
Fruits* fruits[6];
fruits[0] = new Fruits();
// delete[] fruits[0]; // <-- does not work (deletes one object)
// delete[] fruits; // <-- does not work (deletes all objects in the array)
}
How can I do this?
delete fruits[0] will delete that one object for you. delete[] instead serves to delete all non-null elements of that array, but fruits[0] is not an array of objects.
You can't remove an item of an array using standard C++ arrays. Use std::vector instead.
An array like initialized with new[] is a buffer which pointer points at its first memory cell. In vectors and lists, elements are linked. Each element therefore points at its previous and next item, making it easy to remove or insert items. For this purpose, it requires more memory, though.
Example
// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
And just for clarification,
delete fruits[0]
will delete the fruit that is located at the 0th element of the array, but it will not remove it from the array or make the array one element shorter.
You cannot delete what wasn't allocated with new and you can't mix new [] and delete nor new and delete [].
Firstly, you need dynamically allocated space for the elements, no necessarily array of pointers. Removal of an element can be done by shifting all the following elements, so the next element takes the place of the removed element, leaving an unused element at the end of the array, then possibly shrinking the allocated space.
This is practically implemented with std::vector and you shouldn't be implementing it yourself as a beginner. If you're seeking this functionality, use std::vector!
I wish to declare a structure, say with size n=10, that in each i site of the structure there is a vector. The vectors in the sites of the structure have different sizes.
In matlab it can be obtained using struct. how it is done in C++?
I think what you are looking for is an array, or a vector, of vectors.
For example:
#include <array>
#include <vector>
#include <iostream>
int main ()
{
std::array<std::vector<int>, 10> Vectors;
for (auto &i : Vectors) //Loop through all 10 vectors in Vectors
{
for (int j=0; j<5; j++) //Push 1, 2, 3, 4, and 5 into each vector
{
i.push_back(j);
}
}
return 0;
}
struct S {
std::array<std::vector<int>, 10> data;
};
You can also use an alias as well:
using arrayOfTenVectors = std::array<std::vector<int>, 10>;
Of course int is a placeholder. You can make either the struct or the alias a template and use some T inside of the vector.