vector, adding things
-
I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?
Quote:
void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }
-
I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?
Quote:
void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }
Actually, as its name suggestes,
push_back
adds an item at the back of the vector. Try the following sample code in order to seeinsert
in action:#include #include using namespace std;
int main()
{
vector v;v.push_back(1);
v.push_back(2);
v.push_back(3);cout << "v-front " << v.front() << "\n";
cout << "v-back " << v.back() << "\n";v.insert(v.begin(), 4); // insert '4' before the first item: now '4' is the new front of 'v'
cout << "v-front " << v.front() << "\n";
v.insert(v.end() - 1, 5); // insert '5' just before the last item (back) of 'v', now '5' is the second-last item of 'v'
v.insert(v.end(), 6); // insert '6' just before the 'pass-the-end' of 'v', that is '6' is the new back of 'v'.
for (auto x : v) // show, in order, all 'v' items
cout << x << " ";
cout << "\n";
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Actually, as its name suggestes,
push_back
adds an item at the back of the vector. Try the following sample code in order to seeinsert
in action:#include #include using namespace std;
int main()
{
vector v;v.push_back(1);
v.push_back(2);
v.push_back(3);cout << "v-front " << v.front() << "\n";
cout << "v-back " << v.back() << "\n";v.insert(v.begin(), 4); // insert '4' before the first item: now '4' is the new front of 'v'
cout << "v-front " << v.front() << "\n";
v.insert(v.end() - 1, 5); // insert '5' just before the last item (back) of 'v', now '5' is the second-last item of 'v'
v.insert(v.end(), 6); // insert '6' just before the 'pass-the-end' of 'v', that is '6' is the new back of 'v'.
for (auto x : v) // show, in order, all 'v' items
cout << x << " ";
cout << "\n";
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
Thanks CPallini the reason for which the ide wouldn`t accept my insert function was because I switched the order of parameters in the function, the intellisense function helper description is impossible to understand.
-
Thanks CPallini the reason for which the ide wouldn`t accept my insert function was because I switched the order of parameters in the function, the intellisense function helper description is impossible to understand.
-
I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?
Quote:
void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }
-
Hi, Just wanted to point out that you should choose the right STL container. The vector is designed for sequential insertion/access. If you want to frequently insert at both ends then you should consider using a std::deque Best Wishes, -David Delaune
Thanks for your tip, at this point I`m still not sure what I want to use.