Hi
I'm having a little trouble understanding C++ string arguments if that's what you call them.
The below example illustrates.
I'm not sure I understand what line 14 does. The vector 'myvector' is passed to myfunction (called on line 35. I'm assuming string w takes in the first value of the vector (hence myvector.begin()) and moves this begin position to the next position in the vector (post increment).
Does this keep moving the begin() position (hence begin++) as long as there are values in the vector. I'm just not sure how it continually moves through the vector without a loop of some kind.
Also, would anyone know why a pointer (as in 'begin') is used in this situation. Thanks
1. #include <iostream>
2. #include <string>
3. #include <vector>
4. #include <cstdlib>
5.
6. using namespace std;
7.
8.
9. template <typename Iterator>
10. std::string myfunction(Iterator begin, Iterator end)
11. {
12.
13.
14. std::string w(1, *begin++); // I'm not sure what this means. Can't find it
15. //in C++ reference?
16. std::string result = w;
17. std::string entry;
18.
19. for ( ; begin != end; begin++)
20. {
21. int k = *begin;
22. }
23.
24.
25. int main()
26. {
27. vector<char> myvector;
28.
29. for (int i = 0; i < 256; i++)
30. {
31. myvector[i] = string(1,i); //fills vector with
//ASCII values.
32.
33. }
34. // now call myfunction
35. cout << myfunction(myvector.begin(),mvector.end());
36. }