我一直认为这是std::vector的一般智慧是“作为一个数组来实现的”,等等等等。 今天,我去了,测试它,似乎并非如此: 以下是一些测试结果: UseArray completed in 2.619 seconds UseVector completed in 9.284 seconds UseVectorPushBack completed in 14.669 seconds The whole thing completed in 26.591 seconds 那大约要慢3到4倍! 对于“几个毫微秒的vector可能会变慢”的评论并没有真正的理由。 和我使用的代码: #include <cstdlib> #include <vector> #include <iostream> #include <string> #include <boost/date_time/posix_time/ptime.hpp> #include <boost/date_time/microsec_time_clock.hpp> class TestTimer { public: TestTimer(const std::string & name) : name(name), start(boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time()) { } ~TestTimer() { […]
当我这样做: std::vector<int> hello; 一切都很好。 但是,当我把它作为引用的向量时: std::vector<int &> hello; 我得到可怕的错误,如“错误C2528:'指针':参考指针是非法的”。 我想把一堆对struct的引用放到一个向量中,这样我就不需要插入指针了。 为什么矢量这个发脾气? 我唯一的选择是使用指针向量吗?
我有一些代码,如下所示: #include <memory> class Thing; class MyClass { std::unique_ptr< Thing > my_thing; }; 如果我将这个头文件包含在一个不包含Thing类型定义的cpp中,那么这个不会在VS2010-SP1下编译: 1> C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ include \ memory(2067):error C2027:使用未定义的类型'Thing' 用std::unique_ptr std::shared_ptr替换std::unique_ptr并编译。 所以,我猜测这是当前VS2010 std::unique_ptr的实现,需要完整的定义,而且完全依赖于实现。 还是呢? 有没有什么标准的要求,使std::unique_ptr的实现不可能只与前向声明一起工作? 这感觉很奇怪,因为它只应该指向Thing ,不是吗?
我需要从标准输入读取密码,并希望std::cin不要回显用户键入的字符… 我怎样才能禁用从std :: cin的回声? 这里是我目前使用的代码: string passwd; cout << "Enter the password: "; getline( cin, passwd ); 我正在寻找一种操作系统不可知的方式来做到这一点。 这里有两种方法可以在Windows和* nix中执行此操作。
如果我有一个包含逗号分隔数字列表的std :: string,解析出数字并将它们放入整数数组中的最简单方法是什么? 我不想把它推广到解析其他任何东西。 只是一个简单的逗号分隔的整数字符串,如“1,1,1,1,2,1,1,1,0”。