我正在尝试在C++ 11中实现一个带有lambda函数的映射
std::map<int, int, [](const int&a, const int& b) { return a < b; }> test;
Run Code Online (Sandbox Code Playgroud)
但那失败了
错误:模板参数列表中参数3的类型/值不匹配
‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
错误:期望一个类型,得到
‘{}’
错误:
‘;’
令牌之前的声明中的无效类型
有什么建议?
我想学习如何使用新的C++标准库创建多个线程并将其句柄存储到数组中.
我该如何开始一个帖子?
我看到的示例使用构造函数启动一个线程,但如果我使用数组,则无法调用构造函数.
#include <iostream>
#include <thread>
void exec(int n){
std::cout << "thread " << n << std::endl;
}
int main(int argc, char* argv[]){
std::thread myThreads[4];
for (int i=0; i<4; i++){
//myThreads[i].start(exec, i); //?? create, start, run
//new (&myThreads[i]) std::thread(exec, i); //I tried it and it seems to work, but it looks like a bad design or an anti-pattern.
}
for (int i=0; i<4; i++){
myThreads[i].join();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用ZeroMQ进行消息传递的C++应用程序.但它还必须为基于AJAX/Comet的Web服务提供SGCI连接.
为此,我需要一个普通的TCP套接字.我可以通过普通的Posix套接字做到这一点,但是要保持跨平台的便携性并让我的生活更轻松(我希望...)我正在考虑使用Boost :: ASIO.
但现在我有ZMQ的冲突想要使用它自己zmq_poll()
和ASIO它io_service.run()
...
有没有办法让ASIO与0MQ一起工作zmq_poll()
?
或者是否有其他推荐的方法来实现这样的设置?
注:我可以解决,通过使用多线程 - 但它是将与SCGI交通非常低量运行该程序只有一点点的单核/ CPU中,所以多线程是一种资源的浪费?
sizeof char,int,long double ...可能因编译器而异.但是,根据C++ 11或C11标准,我是否有任何有符号和无符号基本积分类型的大小相同的保证?
C++ 11提供了两种类型的特征模板类:std::is_integer
和std::is_integral
.但是,我不能说出它们之间的区别.
什么类型,比如T,可以成std::is_integer<T>::value
真并做出std::is_integral<T>::value
错误?
g ++ 4.7.2是否实现std::set::emplace
,如C++ 11标准所定义并在此处记录?
我写了以下小测试用例:
#include <set>
#include <string>
struct Foo
{
std::string mBar;
bool operator<(const Foo& rhs) const
{
return mBar < rhs.mBar;
}
Foo(const std::string bar) : mBar(bar) {};
};
typedef std::set<Foo> Foos;
int main()
{
Foos foos;
foos.emplace(std::string("Hello"));
}
Run Code Online (Sandbox Code Playgroud)
在G ++ 4.7.2下,这无法编译:
[john.dibling@somewhere hacks]$ g++ -o main.o -std=c++0x -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:19:10: error: ‘Foos’ has no member named ‘emplace’
Run Code Online (Sandbox Code Playgroud)
也无法在IDEOne下编译,但它在MSVC 2012 Update 1下编译.
如何将可变参数模板参数分成两半?就像是:
template <int d> struct a {
std::array <int, d> p, q;
template <typename ... T> a (T ... t) : p ({half of t...}), q ({other half of t...}) {}
};
Run Code Online (Sandbox Code Playgroud) 我有一个perf测试函数的.NET和C++实现,它使用来自6838个键池的字符串键在字典中执行854,750个查找.我编写了这些函数来调查真实应用程序中的性能瓶颈.
.NET实现是用F#编写的,使用Dictionary并为.NET 4.0编译
C++实现使用std :: unordered_map,并在发布模式下使用VS2010构建.
在我的机器上,.NET代码平均运行240毫秒,C++代码运行630毫秒.你能帮我理解速度这个巨大差异的原因是什么?
如果我将C++实现中的密钥长度缩短并使用"key_"前缀而不是"key_prefix_",它将在140毫秒内运行.
我尝试的另一个技巧是用一个自定义的不可变字符串实现替换std :: string,该实现具有指向源的const char*指针和一次性计算的哈希.使用此字符串可以将C++实现的性能降低到190毫秒.
C++代码:
struct SomeData
{
public:
float Value;
};
typedef std::string KeyString;
typedef std::unordered_map<KeyString, SomeData> DictionaryT;
const int MaxNumberOfRuns = 125;
const int MaxNumberOfKeys = 6838;
DictionaryT dictionary;
dictionary.rehash(MaxNumberOfKeys);
auto timer = Stopwatch::StartNew();
int lookupCount = 0;
char keyBuffer[100] = "key_prefix_";
size_t keyPrefixLen = std::strlen(keyBuffer);
/// run MaxNumberOfRuns * MaxNumberOfKeys iterations
for(int runId = 0; runId < MaxNumberOfRuns; runId++)
{
for(int keyId = 0; keyId < MaxNumberOfKeys; keyId++)
{ …
Run Code Online (Sandbox Code Playgroud) 从a转换为a的最佳方法std::array<char, N>
是std::string
什么?
我尝试过生成模板方法,但没有运气.我认为我的C++技能不是最新的.在C++中有这样的惯用方法吗?
是declval<T>()
只为老把戏的替代品(*(T*)NULL)
,以获得T的实例在decltype,而无需担心T的构造函数?
以下是一些示例代码:
struct A {};
struct B {
A a;
};
typedef decltype((*(B*)nullptr).a) T1;
typedef decltype(declval<B>().a) T2;
cout << "is_same: " << is_same<T1, T2>::value << endl;
Run Code Online (Sandbox Code Playgroud)
打印1,因为T1和T2是相同的类型.
如果declval不仅仅是替代品,那么差异是什么?它在哪里有用?