16-3.cpp
来自「effective stl 源代码 code」· C++ 代码 · 共 41 行
CPP
41 行
//
// Yet more examples from Item 16
//
// Fails under MSVC/native lib (lacks range insert)
//
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include "ESTLUtil.h"
int data[] = { -30, 102, 55, -19, 0, 222, -3000, 4000, 8, -2 };
const int numValues = sizeof data / sizeof(int);
void doSomething(const int* pInts, size_t numInts)
{
int sum = 0;
for (size_t i = 0; i < numInts; i++)
sum += pInts[i];
std::cout << "Sum of ints in the array is " << sum << std::endl;
}
int main()
{
using namespace std;
using namespace ESTLUtils;
set<int> intSet; // set that will hold
// data to pass to API
intSet.insert(data, data + numValues); // insert the ints in data
// into the set
vector<int> v(intSet.begin(), intSet.end()); // copy set data into
// a vector
if (!v.empty()) doSomething(&v[0], v.size()); // pass the data to
// the API
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?