stl 找出数组的最小值:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template<typename Type, int size>
Type getMin(const Type (&arr)[size])
{
cout << "Size:" << size <<endl;
vector<Type> vec(arr, arr + size);
sort(vec.begin(), vec.end());
return vec[0];
}
int main(int argc, char *argv[])
{
int arr[] = {4, -3, 2, 4, 5, 3, 4, 3};
cout << getMin(arr) < <endl;
return 0;
}
stl删除重复数据
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template<typename Type>
void display(const vector<Type>& vec)
{
vector<Type>::const_iterator iter = vec.begin();
while(iter != vec.end())
{
cout << *iter <<" ";
iter++;
}
cout <<endl;
}
int main(int argc, char *argv[])
{
const int Len = 12;
int arr[Len] = {2, 2, 3, 2, 1, 2, 3, 4, 5, 4, 5, 6};
vector<int> vec(arr, arr + Len);
sort(vec.begin(), vec.end());
display(vec);
vector<int>::iterator iterBegin = vec.begin();
vector<int>::iterator iterEnd = unique(vec.begin(), vec.end()); //去掉重复的
display(vec);
while(iterBegin != iterEnd)
{
cout << *iterBegin << " ";
iterBegin++;
}
cout <<endl;
return 0;
}
map字符检索
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
map<char, int> mObj;
string str;
cin >> str;
for(int i = 0; i < str.size(); ++i)
{
if(mObj.count(str[i]))
mObj[str[i]]++;
else
mObj.insert(map<char, int>::value_type(str[i], 1));
}
map<char, int>::iterator iter = mObj.begin();
while(iter != mObj.end())
{
cout << iter->first << ":" <<iter->second <<endl;
++iter;
}
return 0;
}