Transform
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ///////////////////////////////
-
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string>
- using namespace std;
-
- int op_increase (int i)
- {
- return i+1;
- }
-
- int op_sum (int i, int j)
- {
- return i+j;
- }
-
- int to_upper(int c)
- {
- if (islower(c))
- {
- return (c-32);
- }
-
- return c;
- }
-
- int to_lower(int c)
- {
- if (isupper(c))
- {
- return c+32;
- }
-
- return c;
- }
-
- int main () {
- vector<int> first;
- vector<int> second;
- vector<int>::iterator it;
-
-
- for (int i=1; i<6; i++) first.push_back (i*10);
-
-
- second.resize(first.size());
- transform (first.begin(), first.end(), second.begin(), op_increase);
- cout << "second contains:";
- for (it=second.begin(); it!=second.end(); ++it)
- {
- cout << " " << *it;
- }
- cout << endl;
-
-
-
- transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);
- cout << "first contains:";
- for (it=first.begin(); it!=first.end(); ++it)
- cout << " " << *it;
- cout << endl;
-
-
-
- string strsrc("Hello, World!");
- string strdest;
- strdest.resize(strsrc.size());
- transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_upper);
- cout << strdest << endl;
-
- transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_lower);
- cout << strdest << endl;
-
-
- return 0;
- }
歡迎關注本站公眾號,獲取更多信息