Can anyone explain me about std::transform???

I wrote this example program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    string s1("Hello");
    transform(s1.begin(),s1.end(),s1.begin(),ptr_fun<int,int>(std::toupper));
    cout<<s1;
    string s2;
    cout<<"Enter something....\n";
    getline(cin,s2);
    transform(s2.begin(),s2.end(),s2.begin(),ptr_fun<int,int>(std::toupper)); // <---
//What do the function parameters mean???
//Especially the 3rd parameter,s2.begin()
    if(s2.compare(s1) == 0)
	cout<<"The strings are same !!! \n";
    else
	cout<<"They are different strings !!! \n";
    return 0;
}


The thing is I dont understand the parameters of std::transform(),I mean to say tht what they are used for.
Can anyone explain this to me pls??? Thnx in advance

Regards,MaBunny.
//Especially the 3rd parameter,s2.begin()

Start by checking references:

http://www.cplusplus.com/reference/algorithm/transform :
result
Output iterator to the initial position of the range where the operation results are stored. The range includes as many elements as [first1,last1).


http://en.cppreference.com/w/cpp/algorithm/transform
d_first - the beginning of the destination range, may be equal to first1 or first2


Specifically in your second call to transform, the third parameter equals the first, and transform is going to store each transformed element at the same position in the sequence where it read it from, that is, it's a transform in-place.

by the way,
if(s2.compare(s1) == 0)

if you don't need three-way compare, don't use it. It's "if(s1 == s2)"
Last edited on
Topic archived. No new replies allowed.