也許這是一個非常愚蠢的問題,但是我正在閱讀的這本書指示我編寫一段代碼,該代碼使用算法對矢量中的元素進行加擾和排序。爲此,本書告訴我使用主C++庫中的算法庫。好吧,到目前爲止我明白了,但是在編寫代碼之後,我想看看如果我從代碼的頂部刪除這個庫,會發生什麼情況,並且讓我感到驚訝的是,一切仍然有效。刪除'#include <algorithm>'不會破壞代碼
這是我正在談論的代碼。當我從代碼的頂部刪除'#include algorithm'時,沒有任何內容會被打破。怎麼會這樣?不使用這個庫時,'random_shuffle'部分是不是應該破壞?
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
vector<int>::const_iterator iter;
cout << "Creating a list of scores.";
vector<int> scores;
scores.push_back(1500);
scores.push_back(3500);
scores.push_back(7500);
cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nFinding a score.";
int score;
cout << "\nEnter a score to find: ";
cin >> score;
iter = find(scores.begin(), scores.end(), score);
if (iter != scores.end())
{
cout << "Score found.\n";
}
else
{
cout << "Score not found.\n";
}
cout << "\nRandomizing scores.";
srand(static_cast<unsigned int>(time(0)));
random_shuffle(scores.begin(), scores.end());
cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nSorting scores.";
sort(scores.begin(), scores.end());
cout << "\nHigh Scores:\n";
for (iter = scores.begin(); iter != scores.end(); ++iter)
{
cout << *iter << endl;
}
system("pause");
return 0;
}
哪個編譯器?如果我在VS2010中刪除'#include',它將無法編譯。 –
Naveen
2012-02-29 12:00:20
我正在使用NetBeans IDE 7.1和MinGW。 – DutchLearner 2012-02-29 12:05:46