| dekeenfrance (64) | |||
|
Hi, I just figured out that some std functions (for example: copy) need the resource and target objects have iterators to work. Otherwise, the compiler rejects. In case of two arrays, declared as:
myA[0] is like a pointer, myB.begin() an iterator, if I do not make any mistake. So, what is exactly the difference between the pointer and the iterator here? They all give the access to elements. If I need the target of copy to be an array like myA which cannot give an iterator, is there a way to make the command "copy" work for it? Thanks in advance for replying. | |||
|
|
|||
| jumper007 (361) | |||
|
Firstly, an iterator is a pointer to the dynamic array. It allows you to go through all its elements. Here is one way of doing it:
Then you can use all the functions that the <array> library can take.Hope I could help, ~ Raul ~ | |||
|
Last edited on
|
|||
| Peter87 (3908) | |
|
An iterator is just an object that can be used to iterate the elements in a container. There are different categories of iterators. The difference is what operations they support e.g. with a Forward iterator you you can use ++ to go from one element to the next and with a Random access iterator you can go from one element to another element in one step. http://www.cplusplus.com/reference/iterator/ myA[0] gives you a reference to the first element in myA. It isn't a pointer, nor is it an iterator. A pointer to an array element is an iterator, because it satisfies all requirements to be a random access iterator. To copy all elements from myA to myB: std::copy(myA, myA + 100, myB.begin());In C++11 you can also use std::begin and std::end that will work the same way on raw arrays and all other containers. std::copy(begin(myA), end(myA), begin(myB)); | |
|
Last edited on
|
|
| jumper007 (361) | |
| @Peter87 Oh, yes. Didn't think that what you said would work. But it seems it does. :) | |
|
|
|
| dekeenfrance (64) | |
|
Thank you for your quick replies. Actually I want to copy from myB to myA. I feel this is a bit more complicated to run... because myA doesn't have iterator and the command "copy" needs one... So maybe in this case I cannot use the std::copy command, I have to write my own function, like jumper007 did... right? | |
|
Last edited on
|
|
| Cubbi (1924) | |
It's the same way: copy(myB.begin(), myB.end(), myA); or copy(begin(myB), end(myB), begin(myA));
| |
|
|
|
| dekeenfrance (64) | |
actually I tried with copy(myB.begin(), myB.end(), myA); but it could not compile. the compiler indicated errors with xutility related to iterators...
| |
|
Last edited on
|
|
| Framework (3237) | |
|
Iterators are usually structures that house a pointer which are given a common interface that containers can use to transverse their elements. Though, this isn't always the case. In some implementations of standard containers, "std::vector::iterator", for example, "iterator" is merely defined as "typedef T* iterator". In effect, the difference between a pointer and an iterator really depends on the implementation of the iterator. Wazzak | |
|
|
|
| Cubbi (1924) | |||
"xutility" indicates that you're using Visual Studio. It does not indicate errors in this case, it raises a warning because using pointers as iterators circumvents the run-time checks Visual Studio adds to the iterators in Debug mode. Add -D_SCL_SECURE_NO_WARNINGS to "Project -> Properties -> C/C++ -> Command Line" or #define it before the #includes(PS: hm, my VS2012 compiles it quietly, perhaps they got rid of that) | |||
|
Last edited on
|
|||
| dekeenfrance (64) | |
| Thanks to you all for your explanations. It's all clear now! :-) | |
|
|
|