這裏是我運動代碼.. 書的問題是重構這個代碼模板(與STL)如何用STL重構這段代碼?
我發現了一些書和谷歌,但我不明白這一點
你能告訴我請舉例? ?
int SumInt(const int* a, int count) {
int result = 0;
for (int i = 0; i < count; ++i) {
result += a[i];
}
return result;
}
float SumFloat(const float* a, int count) {
float result = 0;
for (int i = 0; i < count; ++i) {
result += a[i];
}
return result;
}
void main() {
int intVals[] = {0, 1, 2};
float floatVals[] = {0.F, 1.F, 2.F};
int intTotal = SumInt(intVals, 3);
float floatTotal = SumFloat(floatVals, 3);
....
}
從'std :: vector'開始取出原始數組:'std :: vector intVals {0,1,2};''std :: vector floatVals = {0.F,1.F ,2.F};' –
http://en.cppreference.com/w/cpp/algorithm/accumulate –
如果你的書告訴你'void main',它不是一本C++書。 –