我的C++代码严重依赖于采样(使用rand()),但我希望它是可重现的.所以在开始时,我用随机种子初始化srand()并打印出种子.我希望其他人能够再次运行相同的代码,但用相同的种子初始化srand()并获得与我完全相同的答案.
但在什么情况下有保证?我想只有在同一系统上使用相同的编译器编译二进制文件时才有效?可能使得答案与我最初的答案不同的其他因素有哪些?
我有一个软件库,我曾经创建.a文件,以便人们可以安装它们并链接它们: g++ foo.o -L/path/to -llibrary
但是现在我经常遇到只有.so文件可用的第三方库(而不是.a),你只需要在没有-l开关的情况下链接它们,例如g++ foo.o /path/to/liblibrary.so.
这些解决方案之间有什么区别?我是否应该为我的图书馆用户创建.so文件?
我试图使用set的dynamic_bitset对象,但我在运行时得到一个断言失败:
a.out: boost/dynamic_bitset/dynamic_bitset.hpp:1291:
bool boost::operator<(const boost::dynamic_bitset<Block, Allocator>&,
const boost::dynamic_bitset<Block, Allocator>&)
[with Block = long unsigned int,
Allocator = std::allocator<long unsigned int>]:
Assertion `a.size() == b.size()' failed.
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <iostream>
#include <set>
#include <boost/dynamic_bitset.hpp>
int main() {
typedef boost::dynamic_bitset<> bitset;
std::set<bitset> myset;
bitset x(2, 0);
bitset y(3, 1);
myset.insert(x);
myset.insert(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么插入的dynamic_bitset对象需要相同的大小。为了operator<起作用,是否不能假设较短位集中的最高有效位隐式地用零填充?
有什么办法可以使这套dynamic_bitsets起作用?
我也尝试过a,unordered_set因为它不需要the,operator<但是由于dynamic_bitset没有a hash_value而不能编译,而且我不确定如何在不使用其to_ulong成员函数的情况下编写该函数,该函数仅适用于短位集。
我想迭代字符串中的标记:
#include <boost/algorithm/string.hpp>
using namespace boost::algorithm;
typedef split_iterator<std::string::const_iterator> SplitIter;
for(SplitIter i = make_split_iterator(str, token_finder(is_space()));
i != SplitIter(); ++i)
std::cout << *i << std::endl; // prints token
Run Code Online (Sandbox Code Playgroud)
这种使用 的方法split_iterator对我来说是理想的,因为它不会将匹配复制到向量中,而只是迭代它找到的范围,因此非常有效。
现在,我如何修改它,以便分割迭代器开始搜索str[5],例如,而不是字符串的开头?当然,因为我想避免不必要的复制,所以我不想在子字符串 copy 上进行搜索str.substr(5)。
处理NULL指针时可以做到这一点
if(ptr != NULL){ ... }
or this
if(ptr != 0){ ... }
Run Code Online (Sandbox Code Playgroud)
是否有理由偏爱另一个C++?
我有一个模板化的函数fct,它使用一些基于模板参数的复杂数据结构.它还调用一些独立helpers命名空间中的辅助函数(模板化在同一类型上),并使用相同的复杂数据结构.现在它变得非常难看,因为我们无法typedef为所有函数都可以访问的复杂类型创建一个:
namespace helpers {
template<class T>
void h1(const std::vector< std::vector< std::map<T, std::set<T> > > >& bar){
// ...
}
}
template<class T>
void fct(const std::vector< std::vector< std::map<T, std::set<T> > > >& bar){
// ...
helpers::h1(bar);
}
Run Code Online (Sandbox Code Playgroud)
现在我想通过使用一个所有函数都可以使用的typedef来使它更漂亮.
模板化typedef会很好,但不允许:
template<class T>
typedef std::vector< std::vector< std::map<T, std::set<T> > > > Bar;
Run Code Online (Sandbox Code Playgroud)
我认为另一个解决方案是将所有这些函数包装在模板中namespace,但在C++中也是不允许的(我听说它将在`C++ 0x'...中).
我们当然有模板化的类,但请注意,我并不是真的希望用户必须构造一个对象并在其上调用成员函数.所以我最终使用的解决方法是使用模板化的类,其中所有成员函数都是static:
template<class T>
class All {
typedef std::vector< std::vector< std::map<T, std::set<T> > > > Bar;
static void …Run Code Online (Sandbox Code Playgroud) 我如何要求和检查的说法是有一定概念的C++?
例如,标头中的random_shuffle函数algorithm要求其参数是RandomAccessIterators:
template<typename _RandomAccessIterator>
inline void
random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
// concept requirements
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__glibcxx_requires_valid_range(__first, __last);
if (__first != __last)
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
}
Run Code Online (Sandbox Code Playgroud)
我想我不能__glibcxx_function_requires在我自己的代码中使用这些等等?他们是如何工作的?你在代码中检查这样的东西吗?
在C++,为什么以下元素访问vector无效?
void foo(std::vector<int>* vecPtr) {
int n = vecPtr->size(); // ok
int a = vecPtr->[0]; // invalid
}
Run Code Online (Sandbox Code Playgroud)
相反,我们必须编写更繁琐的内容
(*vecPtr)[0] = 1;
Run Code Online (Sandbox Code Playgroud)
我认为,operator[]调用应该具有与方法调用相同的语法,并且我讨厌额外的星和括号.(我知道C++有很多严重的问题,但每当我输入它时,这个问题都会让我烦恼...)
我在哪里可以为Go语言提交错误报告?我的意思是报告关于崩溃的编译器或关于Go的其他东西不能像宣传的那样工作.阅读它们也很有趣.