Hi,
when i use the push_back function to add an element at the end of a vector, is vector resized automatically (if necessary) or should i check it out?
Thanks..
Hi,
when i use the push_back function to add an element at the end of a vector, is vector resized automatically (if necessary) or should i check it out?
Thanks..
It is resized for you.
Those containers do everything for you to make sure things don't go wrong, and if something does go wrong, an exception is thrown.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
>> if something does go wrong, an exception is thrown.
Not necessarily. For example, if you use an index outside of the vector's bounds with at(), it will through an exception, but if you do it with operator[] you will get undefined behavior.
Good point.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Got it. Thanks.
May someone tell me how i should use the "using" declaration to declare the push_back function?
I try the following but i get a compilation error:
And what about if i want to use a vector for a data type that i define later in the file?Code:using std::vector<int>::push_back;
I used the "extern" declaration but i also got a compilation error
Thanks in advance!
Why do you think you need to "declare" the push_back function? The only case I can think of when you might want to do this is if you were inheriting from vector.
>> And what about if i want to use a vector for a data type that i define later in the file?
I believe the vector needs the full type information of the type it holds. You should be able to change your code so that the vector is declared after the type. If you absolutely cannot, then you will probably have to store a pointer to the datatype in the vector.
Yeah, right, i didn't make myself clear. I just want to use the function, so i'll have to use the "using" declaration before i use itOriginally Posted by Daved
>> I just want to use the function, so i'll have to use the "using" declaration before i use it
No, you don't need to use a using declaration before you use a function.
You just have to make sure you've included the appropriate header that has the function declaration. In this case that's <vector>. If you're getting a specific error, feel free to post the error message and code it refers to.
Well, as far as i know vector class is declared in the std namespace. And whenever i need to use a class or a function out of its namespace i shall use the "using" declaration before.Originally Posted by Daved
(Obviously you disagree so i must be wrong; i'm just telling you what i have understood. Please correct me if possible)
Whenever you use a class or a function from a namespace, you must either fully qualify it or use a using declaration or statement to make it visible, true.
But push_back is not in the std namespace, it's in the vector class.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
http://www.cprogramming.com/tutorial/stl/vector.htmlOriginally Posted by CornedBee
"Moreover, the vector class is part of the std namespace, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program."
By using the statement
i have access to every function declared in the vector class?Code:using std:: vector;
Last edited by tezcatlipooca; 12-19-2006 at 07:51 PM.
No, to use the vector, there's two way:
For example:
1.
Code:#include <vector> int main() { std::vector<int> myVector; myVector.push_back(int); return 0; }
2.
Here's some reading about namespace: http://www.cprogramming.com/tutorial/namespaces.htmlCode:#include <vector> using namespace std; int main() { vector<int> myVector; myVector.push_back(int); return 0; }
Last edited by g4j31a5; 12-19-2006 at 08:49 PM.
ERROR: Brain not found. Please insert a new brain!
“Do nothing which is of no use.” - Miyamoto Musashi.
However the following code compiles and runs normally:
What i understand is that the following statementCode:#include <iostream> using std::cout; #include <vector> using std::vector; int main(){ vector<int> x; x.push_back(3); x.push_back(5); cout<<"x[0]="<<x.at(0)<<" x[1]="<<x.at(1); system("pause"); return 0; }
brings the whole std::vector class in scope so i am able to use any function declared in it.Code:using std::vector;
(Please correct me if wrong)
It brings the name vector into scope such that you no longer have to prefix it with a std::brings the whole std::vector class in scope so i am able to use any function declared in it.
You will still not be able to directly use all private (and protected) members of std::vector, whether member function or member variable.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)