我正在使用全局变量实现线程间通信.
//global var
volatile bool is_true = true;
//thread 1
void thread_1()
{
while(1){
int rint = rand() % 10;
if(is_true) {
cout << "thread_1: "<< rint <<endl; //thread_1 prints some stuff
if(rint == 3)
is_true = false; //here, tells thread_2 to start printing stuff
}
}
}
//thread 2
void thread_2()
{
while(1){
int rint = rand() % 10;
if(! is_true) { //if is_true == false
cout << "thread_1: "<< rint <<endl; //thread_2 prints some stuff
if(rint == 7) …Run Code Online (Sandbox Code Playgroud) 我需要以两种不同的方式定义get方法.一个用于简单类型T.一个用于std :: vector.
template<typename T>
const T& Parameters::get(const std::string& key)
{
Map::iterator i = params_.find(key);
...
return boost::lexical_cast<T>(boost::get<std::string>(i->second));
...
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能为std :: vector专门化这个方法.因为代码应该看起来像这样:
template<typename T>
const T& Parameters::get(const std::string& key)
{
Map::iterator i = params_.find(key);
std::vector<std::string> temp = boost::get<std::vector<std::string> >(i->second)
std::vector<T> ret(temp.size());
for(int i=0; i<temp.size(); i++){
ret[i]=boost::lexical_cast<T>(temp[i]);
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何专门为此功能.非常感谢.
我们目前正在处理我班级的哈希函数.我们的教练要求我们在互联网上使用哈希函数来比较我们在代码中使用的哈希函数.
第一个:
int HashTable::hash (string word)
// POST: the index of entry is returned
{ int sum = 0;
for (int k = 0; k < word.length(); k++)
sum = sum + int(word[k]);
return sum % SIZE;
}
Run Code Online (Sandbox Code Playgroud)
第二:
int HashTable::hash (string word)
{
int seed = 131;
unsigned long hash = 0;
for(int i = 0; i < word.length(); i++)
{
hash = (hash * seed) + word[i];
}
return hash % SIZE;
}
Run Code Online (Sandbox Code Playgroud)
SIZE为501(哈希表的大小),输入来自20,000多个单词的文本文件.
我用一些代码示例看到了这个问题,但是并不确定在哈希函数中要查找什么.如果我理解正确,在我的情况下,哈希采用输入(字符串)并进行数学计算以为字符串分配数字并将其插入表中.这个过程是为了提高搜索列表的速度吗?
如果我的逻辑是合理的,有没有人有一个很好的例子或资源显示一个涉及字符串的不同哈希函数?甚至是编写我自己的高效哈希函数的过程.
C++ 11定义high_resolution_clock并且它具有成员类型period和rep.但我无法弄清楚如何才能获得该时钟的精度.
或者,如果我可能达不到精度,我能以某种方式至少得到一个在纳秒之间的最小可表示持续时间的计数吗?可能用period?
#include <iostream>
#include <chrono>
void printPrec() {
std::chrono::high_resolution_clock::rep x = 1;
// this is not the correct way to initialize 'period':
//high_resolution_clock::period y = 1;
std::cout << "The smallest period is "
<< /* what to do with 'x' or 'y' here? */
<< " nanos\n";
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个在C++中为0或1的随机int.现在,每次运行此代码时都会收到0,我不知道为什么.这有什么问题?
#include <ctime>
#include <cstdlib>
srand(time(0));
int randomval = rand() % 2;
cout << randomval << endl;
Run Code Online (Sandbox Code Playgroud) 在CRTP模式中,如果我们想要将派生类中的实现函数保持为受保护,则会遇到问题.我们必须将基类声明为派生类的朋友或使用类似的东西(我没有尝试过链接文章的方法).是否有其他(简单)方法允许将派生类中的实现函数保持为受保护?
编辑:这是一个简单的代码示例:
template<class D>
class C {
public:
void base_foo()
{
static_cast<D*>(this)->foo();
}
};
class D: public C<D> {
protected: //ERROR!
void foo() {
}
};
int main() {
D d;
d.base_foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出error: ‘void D::foo()’ is protected了g ++ 4.5.1,但是如果protected被替换为compiles public.
我正在使用boost.serialization.一些示例代码在serialize方法中使用BOOST_SERIALIZATION_NVP:
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
ar & BOOST_SERIALIZATION_NVP(_from_prop);
}
Run Code Online (Sandbox Code Playgroud)
我试图谷歌它的功能,但没有找到任何有用的东西.什么是差异
ar & BOOST_SERIALIZATION_NVP(_from_prop)
Run Code Online (Sandbox Code Playgroud)
和
ar & _from_prop?
Run Code Online (Sandbox Code Playgroud) 我有一个未知大小的元组(它是方法的模板参数)
是否可以获得它的一部分(我需要丢掉它的第一个元素)
例如,我有tuple<int,int,int>(7,12,42).我想要tuple<int,int>(12,42)在这里
为什么这个C++代码不能在VS2010下编译:
for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {}
Run Code Online (Sandbox Code Playgroud)
虽然这个做了:
short b = 0;
for ( int a = 0; a < 10; ++a, ++b ) {}
Run Code Online (Sandbox Code Playgroud)
是否禁止在for-loop初始化程序中声明两个不同类型的变量?如果是这样,你怎么解决它?