c++STL 상용 스 트 리밍 알고리즘
1849 단어 c + +stl스 트 리밍 알고리즘
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++알고리즘 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 원활 공사 & & 원활 공사 (차 트 법)모 성 은 도시 의 교통 상황 을 조사 하여 기 존의 도시 도로 통계 표를 얻 었 고 표 에는 모든 도로 가 직접 연 결 된 도시 가 열거 되 어 있다.성 정부의 '원활 한 공사' 목 표 는 성 전체의 어느 두 도시...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.