c++STL 상용 스 트 리밍 알고리즘

헤더 파일 을 가 져 와 야 합 니 다\#include
1.for_each

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

class MyPrint {
public:
 void operator()(int val) const{
  cout << val << " ";
 } 
};

void printVector(int val) {
 cout << val << " ";
}

void test() {
 vector<int> v1;
 for (int i = 0; i < 10; i++)
 {
  v1.push_back(i);
 }
 //      
 for_each(v1.begin(), v1.end(), printVector);
 cout << endl;
 //     
 for_each(v1.begin(), v1.end(), MyPrint());
 cout << endl;
}


int main() {
 test();
 system("pause");
 return 0;
}
2.transform:용 기 를 다른 용기 로 옮 깁 니 다.

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Transform {
public:
 int operator()(int val) const{
  //     val      
  return val;
 } 
};
class MyPrint {
public:
 void operator()(int val) const {
  cout << val << " ";
 }
};

void test() {
 vector<int> v1;
 for (int i = 0; i < 10; i++)
 {
  v1.push_back(i);
 }
 vector<int> v2;
 //           
 v2.resize(v1.size());
 transform(v1.begin(), v1.end(), v2.begin(), Transform());
 for_each(v2.begin(), v2.end(), MyPrint());
 cout << endl;
}


int main() {
 test();
 system("pause");
 return 0;
}
이상 은 c++STL 에서 자주 사용 하 는 알고리즘 에 대한 상세 한 내용 입 니 다.c++알고리즘 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기