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>
6.         using namespace std;
9.          template <typename Iterator>
10.          std::string myfunction(Iterator begin, Iterator end) 
11.         {
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;
19.         for ( ; begin != end; begin++)            
20.           {
21.              int k = *begin;
22.           }
25.          int main()
26.           {
27.             vector<char> myvector;
29.             for (int i = 0; i < 256; i++)
30.             {
31.            myvector[i] = string(1,i); //fills vector with
                                           //ASCII values.
33.             }
34.               // now call myfunction
35.    cout << myfunction(myvector.begin(),mvector.end());
36.    }

Recommended Answers

All 2 Replies

You can usually find online help with most c++ functions and classes by googlein for them. In this case, google for "c++ std::string" and this will be the first hit you see.

Once there, click the link (constructor)

Next, look down the page at Parameters and you will find this (where n is the first parameter and c is the second):

Number of characters to copy.
Character to fill the string with. Each of the n characters in the string will be initialized to a copy of this value.

In otherwords, line 14 is creating a std::string object of 1 character and initializing it with whatever *being points to.

lines 19-22 loops through the rest of begin, but to me that loop isn't doing much of anything. I don't see any purpose of that loop.

Thanks for the explanation.. I googled string arguments so thats why I couldn't find anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.