根据这个cplusplus.com页面,std::copy它位于<algorithm>标题中,std::swap但是这样可行:
#include <iostream>  // std::cout
#include <vector>  // std::vector
#include <iterator>  // std::ostream_iterator()
#include <cstdlib>  // rand(), srand()
// NOT including <algorithm>
int main()
{
  srand(time(NULL));
  const int SIZE = 10;
  std::vector<int> vec; 
  for(int i = 0; i < SIZE; ++i)
  {
     vec.push_back(rand() % 256);
  }
  copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << "\n";
}
我唯一能想到的就是它们也是出口的<vector>......但是为什么我们需要<algorithm>标题呢?
<vector>您在此处使用的特定实现可能包括copy和的定义swap(可能通过包含<algorithm>或可能包含其他包含它们的其他私有标头),但这只是一个实现细节,并不保证是可移植的.完全有可能的是,如果你要切换编译器,你最终会使用C++标准库的实现,copy而swap不是在那里导入<vector>,在这种情况下你的代码将不再编译.
换句话说,仅仅因为它恰好在你的编译器上工作并不意味着它是可移植的,所以为了最大的可移植性和正确性你应该包括在内<algorithm>.
希望这可以帮助!
| 归档时间: | 
 | 
| 查看次数: | 1147 次 | 
| 最近记录: |