hi, is push_back() a overloaded function?
bcoz, probabily push_back() accepts simple argument and also pointer both.
is it a overloaded function ? if so, is there any other way acceptance rule ?
Printable View
hi, is push_back() a overloaded function?
bcoz, probabily push_back() accepts simple argument and also pointer both.
is it a overloaded function ? if so, is there any other way acceptance rule ?
ok,....it is written void push_back( const TYPE &val );
so, pass by reference (alias).
i have written a small code to see the behaviour...
Code:#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> z;
int* p = new int[3];
p[0]=6;
z.push_back(p); // illegal
z.push_back(p[0]);
cout<<z[0]<<endl;
}
Now suppose the vector contains array of pointers (say vector<int* > ) then how do i insert pointers into the vector ?? push_back() function wont allow me to enter pointer :confused: so whats the way to enter pointer into the vector ?
try this:
Code:#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> z;
int* p = new int[3];
p[0]=6;
z.push_back(*p);
cout<<z[0]<<endl;
return 0;
}
it basically works the same way as if you would try to do this:
Code:cout << p << endl; //this wont give you an error on most compilers
//but will print out some gibberish
cout << *p << endl; //if you do this, the program will print out the first element
//of the array that p points to, hence, 6
no that code was an example. my las question was not meant for that.
forget about my code.
my question > is it possible to create a vector<some_type *> ?? how do i insert pointers into the vector ?
say i want to do something like....
vector{ p1,p2,p3,p4} // where p1,p2,p3,p4...etc are pointers to something.
is it possible ?
i am learning . any help is appreciated.
thanks
Code:#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int *> z; // create a container for pointer to int
int* p = new int[3];
p[0]=6;
z.push_back(p); // push a pointer to int
cout<< *z[0] <<endl; // use * to print the value of int
}
Yes, and the syntax is just as you have it. Say you wanted to create a vector of pointers to int:Quote:
is it possible to create a vector<some_type *>
Just remember that the regular warnings about pointers still apply. If they point to local variables, then they become invalid when the function exits. If they point to dynamically allocated memory, then you have to remember to free them when you are done.Code:int num = 10;
vector<int*> vec;
vec.push_back(&num);
-tf
I think it would be a problem trying to put an array of integers into a vector because it wouldn't know how large the array is. If you are using vectors you might as well do something like this:
You might, however, be able to use a char array, provided it is null-terminated:Code:#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<vector<int> > z;
vector<int> y;
y.push_back(0);
y.push_back(1);
z.push_back(y);
cout<<z[0][0]<<endl;
cout<<z[0][1]<<endl;
}
Code:#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<char* > z;
char y[2];
y[0]='a';
y[1]='b';
y[2]='\0';
z.push_back(y);
cout<<z[0]<<endl;
cout<<z[1]<<endl;
}
just a quick question by looking ur code.
Code:int num = 10;
vector<int*> vec;
vec.push_back(&num); // u r sending a pointer(address ) but push back demands alias . pointer(here by using & ) and references are not same thing.
I believe alphaoide's solution will work...I wasn't thinking that the vector doesn't need to know the size, it just has the address of the first element in the array. You just need to dereference it or use the subscript operator as if you are working with an ordinary array.
>>I believe alphaoide's solution will work.....
its giving compile error. i already checked that in my system . i copied and pasted.
edit: i have ideas on ur example. i was curious to know how can i create vector<some_type *>.
What compile error? It works on mineQuote:
Originally Posted by blue_gene
g++ compiler....
it works on both my vc++ 6.0 and g++