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?
#include <iostream>
#include <array>
int main()
{
int i = 0; // Counter for number of elements in static array "myA"
double myA[5] = { 1, 2, 3, 4, 5}; // Static array
std::array<double,5> myB; // Dynamic array
std::array<double,5>::iterator it; // Iterator to dynamic array
// Iterate through the dynamic array, but keeping the counter of the elements at same step.
// Each element in the myB is getting the value of the element myA[i] going in an ascending order
for (it = myB.begin(), i = 0; it != myB.end(); ++it, ++i)
{
*it = myA[i]; // Assign value
}
for (it = myB.begin(); it != myB.end(); ++it) // Iterate once more to print them out
{
std::cout << *it << " "; // Print them out
}
std::cout << std::endl;
system("pause");
return 0;
}
Then you can use all the functions that the <array> library can take.
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));
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?
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.
the compiler indicated errors with xutility related to iterators.
"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)