![]() |
|
#1
|
|||
|
|||
Vector IssuesHello all C/C++ expert programmer,
i have tremendous problem about vector.
Ans : Array is fixed size. We cannot copy array. Vector is a kind of dynamic data structure. (I don't know whether this it true because i just guess) I have googling it and find in the book but i also couldn't understand. I think i should smash my head to the wall or jump from Taipei 101. Below is my program : CPP / C++ / C Code:
Why the loop is not stop even though it is over the size ? How we can prompt user enter a number into vector and reverse the number ? Example: 1234 Answer:4321 Thanks for your help. Your help is greatly appreciated by me and others who start learning C++. Last edited by admin : 13-May-2007 at 09:52.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|
#2
|
||||
|
||||
Re: Vector IssuesThe loop will not stop, because you have:
CPP / C++ / C Code:
EDIT: Here are a couple of websites that might better explain some of what you seek: http://www.codeproject.com/vcpp/stl/stlintroduction.asp http://www.yolinux.com/TUTORIALS/Lin...ialC++STL.html Reversing the elements can be done something like this: CPP / C++ / C Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! Last edited by TurboPT : 13-May-2007 at 08:20.
|
|
#3
|
||||
|
||||
Re: Vector IssuesQuote:
A vector is a Sequence that supports random access to elements, constant time insertion and removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a vector may vary dynamically; memory management is automatic. Vector is the simplest of the STL container classes, and in many cases the most efficient. ...source: www.sgi.com Quote:
Iterators are a generalization of pointers: they are objects that point to other objects. As the name suggests, iterators are often used to iterate over a range of objects: if an iterator points to one element in a range, then it is possible to increment it so that it points to the next element. ...source: www.sgi.com Quote:
You can copy an array, but you have to know a lot about it. You are also responsible for the memory allocation. Vector is not a dynamic data structure. It is a Sequence (a generic Container) of elements owned by the vector. The elements are dynamically allocated by the vector during storage. They are also automatically deallocated by the vector upon removal from the container. Quote:
Try this: CPP / C++ / C Code:
Code:
:davis: |
|
#4
|
|||
|
|||
Re: Vector IssuesI feel shameful because always ask simple question rather than do my own research.
By the way, all the explanation is short and simple. This had make me understand easier and have not feel frustrated to learn C++. Thanks. Your help is greatly appreciated by me and others. A billion thanks to you. |
|
#5
|
|||
|
|||
Re: Vector IssuesMy program has some problem. The loop is not stop after the size.
Below is my program : CPP / C++ / C Code:
I have a question. 1. If i have class A[10], i would like iterate the object A[10]. I will declare A::iterator itera; 2. If i have list<int>aList, i would like iterate the list. I will declare aList::iterator itera; I don't know whether this is true because i just guess. Thanks. Last edited by admin : 14-May-2007 at 05:45.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|
#6
|
|||
|
|||
Re: Vector IssuesHere is another program which simulate 2D array using vector.
I not really fully understand the program. Therefore, your help is greatly appreciated by me and others. Below is my program: CPP / C++ / C Code:
Thanks for your help. Your help is greatly appreciated by me and others. |
|
#7
|
|||
|
|||
Re: Vector IssuesQuote:
If you are going to be using the "vector of vectors" as 2-D array, why not use array notation throughout? Understanding iterators is something to learn, but, in the meantime, why not use simpler notation: CPP / C++ / C Code:
Here is a sample program run: Code:
Regards, Dave |
|
#8
|
|||
|
|||
Re: Vector IssuesI have know array notation and pointer for 2D arrary. Due to this reasons, i would like to try out the new things.
Pointer Notation in C: Code:
Thanks for your help. Your help is greatly appreciated by me and others. |
|
#9
|
|||
|
|||
Re: Vector IssuesQuote:
One of the most fundamental facts of C (and C++) pointer/array notation is the following: If p is a pointer data type and m is an integer data type, then the following two notations are semantically identical. Not "almost the same", not "usually the same", not "depending on the context", but identical This is a definition of the notation. CPP / C++ / C Code:
Note that this does not (not) imply that "arrays are the same thing as pointers". It's just notation. Now, in C (and C++) we don't really have 2-Dimensional arrays. We have arrays of arrays. However, since 2-Dimensional arrays are so useful, the array notation defined above also extends to more than one dimension. Suppose pp is a "pointer to pointer" data type, and n is an integer data type. Then the array notation defined above can be extended to the following definition: CPP / C++ / C Code:
But we know that CPP / C++ / C Code:
CPP / C++ / C Code:
Regards, Dave Left as an exercise for the student: Would you like to try for 3-Dimensional array notation using a "pointer to pointer to pointer?" |
|
#10
|
|||
|
|||
Re: Vector IssuesI think you have misunderstand what i want. Sorry to say so.
Basically, i have two question regarding the uses of vector and iterator. Below is my problem : 1. Why the loop is not loop when i using push_back function. CPP / C++ / C Code:
2. Why when i display the vector of vector, i need the column notation and not row notation. Sorry for my poor English. CPP / C++ / C Code:
I just post part of the code because this is a very simple program and to shorten the time you analyze the code. I hope you are willing to help a stupid person. Thanks for your help. Your help is greatly appreciated by me and others. GOD will blessed you all. Last edited by admin II : 18-May-2007 at 15:38.
Reason: Please surround your CPP code with [CPP] ... [/CPP]
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The