Simple example using built-in data structure,
#include <vector>
#include <string>
#include <functional>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string v;
cin>>v;
transform(v.begin(),v.end(),v.begin(),[](char c){return toupper(c);});
cout<<v<<endl;
return 0;
}
One more complex example, using user defined student class, for each student, add extra 5 points to its grade.
#ifndef STUDENT_HPP_INCLUDED
#define STUDENT_HPP_INCLUDED
using namespace std;
class student{
private:
int NO;
int Grade;
string Name;
public:
student():NO(0),Grade(0),Name(""){}
student(int NO_, int Grade_, string Name_):NO(NO_),Grade(Grade_),Name(Name_){}
student(const student& s){
this->setNO(s.getNO());
this->setGrade(s.getGrade());
this->setName(s.getName());
}
int getNO() const { return NO; }
int getGrade() const { return Grade; }
string getName() const { return Name; }
void setNO(int NO_) { NO = NO_; }
void setGrade(int Grade_) { Grade = Grade_; }
void setName(string Name_) { Name = Name_; }
bool operator==(student& s) const {
return this->getNO() == s.getNO();
}
bool operator<(student& s) const {
return this->getGrade() < this->getGrade();
}
void print(){
cout << NO << "\t" << Name << "\t" << Grade << endl;
}
};
#endif // STUDENT_HPP_INCLUDED
The main function
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
#include <./student.hpp>
using namespace std;
int main(){
vector<student> sv;
student s1(1001, 70, "Sam");
student s2(1002, 85, "Tim");
student s3(1003, 90, "James");
student s4(1004, 50, "David");
sv.push_back(s1);
sv.push_back(s2);
sv.push_back(s3);
sv.push_back(s4);
cout << "Original Students Vector: " << endl;
auto itr = sv.begin();
while(itr!=sv.end()){
itr->print();
itr++;
}
// transform the grade
transform(sv.begin(), sv.end(), sv.begin(), [](student s){s.setGrade(s.getGrade()+5); return s;});
cout << "After transform, the original: " << endl;
itr = sv.begin();
while(itr!=sv.end()){
itr->print();
itr++;
}
return 0;
}
Note that the destiny could be the same with the source.