小编Neg*_*ero的帖子

C++ 11初始化地图

我正在尝试使用C++ 11语法初始化STL映射,但这似乎不起作用.初始化后,当我尝试访问该元素时,它会尝试调用Foo的私有构造函数.我错过了什么?如果我使用它,它的工作原理.我想知道我是否可以使用operator []来访问初始值...

#include <map>
#include <string>

class Foo{
public:
    int a, b;
    Foo(int a_, int b_){
        a = a_;
        b = b_;
    }

private:
    Foo(){};
};


int main(){

    std::map<std::string, Foo> myMap = { {"1", Foo(10,5)}, {"2", Foo(5,10)} };
    int b  = myMap["1"].b;    // it tries to call private constructor of Foo.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ map c++11

13
推荐指数
2
解决办法
1万
查看次数

使用stdlibc ++ 4.7启用C++ 11时,clang错误输出,而gcc编译正常

我一直在尝试让C++ 11工作,在浏览了不同的网站和Q/A之后,我仍然遇到了麻烦.我想和libstdc ++一起使用clang.它在clang状态中表示它受补丁支持 - http://clang.llvm.org/libstdc++4.7-clang11.patch.我从macports下载gcc4.7并在gcc4.7的头文件中做了相应的更改

我不使用libc ++的原因是因为libc ++和libstdc ++之间的ABI兼容性,由这个线程表示:为什么不能用c ++ 0x模式中的libc ++来链接这个boost :: program_options示例?

好的,一切都完成后,我使用以下代码测试了我的设置:

#include <mutex>
#include <thread>

int main ( ) {
    std::mutex myMutext;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待include应该在c ++ 11下工作.

所以这就是我用GCC编译它的方法

g++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main
Run Code Online (Sandbox Code Playgroud)

编译成功

与Clang

clang++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

@work:boostTest$ clang++ -std=c++11 -I/opt/local/include/gcc47/c++ -L/opt/local/lib/gcc47 main.cpp -o main
In file included from main.cpp:1:
In file included from /opt/local/include/gcc47/c++/mutex:38:
In file included from /opt/local/include/gcc47/c++/tuple:37:
In file included from /opt/local/include/gcc47/c++/utility:70: …
Run Code Online (Sandbox Code Playgroud)

c++ macos gcc clang c++11

9
推荐指数
2
解决办法
2万
查看次数

这个"<>"在模板类函数中意味着什么?

有这样的代码.

const std::string DeviceTypeStrings[] ={ "A", "B", "C", "D", "E" };

enum DeviceTypes { A = 0, B, C, D, E };

template <DeviceTypes T> class DeviceType;
template <DeviceTypes T> std::ostream& operator<< (std::ostream& output, const DeviceType<T>& dev);

template <DeviceTypes T> class DeviceType {
    public:
         static const int value = T;
         static const std::string string;
         friend std::ostream & operator<< <>(std::ostream & output, const DeviceType<T> & deviceType );
};
template <DeviceTypes T> const std::string DeviceType<T>::string = DeviceTypeStrings[T];
template <DeviceTypes T> std::ostream & …
Run Code Online (Sandbox Code Playgroud)

c++ templates

7
推荐指数
1
解决办法
257
查看次数

Bluetooth Passkey如何再次保护MITM攻击

我正在阅读蓝牙低功耗:开发人员手册,并对MITM的保护感到困惑.

书中说,在交换配对信息后,双方都会生成一个随机数.连同该随机数一起计算确认值.然后在双方之间交换确认值,随后显示随机数.

交换的消息在配对中就像这样.

[Apr 25 18:04:43.919]  [SMP Send]  LE SMP Pairing Request Command
[Apr 25 16:53:09.005]  [SMP Receive]  LE SMP Pairing Response Command 
[Apr 25 16:53:09.019]  [SMP Send]  LE SMP Pairing Confirm Command 
[Apr 25 16:53:14.016]  [SMP Receive]  LE SMP Pairing Confirm Command 
[Apr 25 16:53:14.017]  [SMP Send]  LE SMP Pairing Random Command 
[Apr 25 16:53:14.076]  [SMP Receive]  LE SMP Pairing Random Command 
Run Code Online (Sandbox Code Playgroud)

该书声称这可以防止MITM攻击,因为MITM必须猜测2 ^ 128个可能的随机数来计算确认值.

我的问题是,这有助于防止MITM攻击.我的意思是,如果我是MITM,我只需将确认号码从一端传递到另一端,甚至不需要计算.

我确信我一定错过了什么.

security bluetooth bluetooth-lowenergy

7
推荐指数
1
解决办法
1392
查看次数

向Lasagne神经网络层添加偏差

我想知道是否有办法在Lasagne神经网络工具包中为每一层添加偏置节点?我一直试图在文档中找到相关信息.

这是我构建的网络,但我不知道如何向每个层添加偏向节点.

def build_mlp(input_var=None):
    # This creates an MLP of two hidden layers of 800 units each, followed by
    # a softmax output layer of 10 units. It applies 20% dropout to the input
    # data and 50% dropout to the hidden layers.

    # Input layer, specifying the expected input shape of the network
    # (unspecified batchsize, 1 channel, 28 rows and 28 columns) and
    # linking it to the given Theano variable `input_var`, if any:
    l_in = lasagne.layers.InputLayer(shape=(None, 60), …
Run Code Online (Sandbox Code Playgroud)

python neural-network theano lasagne

7
推荐指数
1
解决办法
852
查看次数

Python numpy loadtxt 因日期时间而失败

我正在尝试使用 numpy loadtxt 将 csv 文件加载到数组中。但我似乎无法正确加载日期时间。

下面演示了正在发生的情况。我做错什么了吗?

>>> s = StringIO("05/21/2007,03:27")
>>> np.loadtxt(s, delimiter=",", dtype={'names':('date','time'), 'formats':('datetime64[D]', 'datetime64[m]')})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 573, in <lambda>
  return lambda x: int(float(x))
ValueError: invalid literal for float(): 05/21/2007
Run Code Online (Sandbox Code Playgroud)

python datetime numpy

5
推荐指数
1
解决办法
2万
查看次数

为什么1不大于-0x80000000

为什么1不大于-0x80000000.我知道它与溢出有关.但有人可以解释为什么吗?是0x80000000不是常数我认为是吗?

assert(1 > -0x80000000);
Run Code Online (Sandbox Code Playgroud)

断言在C++中触发.这是为什么?


我很感激所提供的一些答案.但是C++标准是否定义了常量需要存储在32位整数中?为什么编译器没有认识到80000000不适合32位整数并且使用64位呢?我的意思是,最大的32位int可以是0x7FFFFFFF.0x80000000明显大于那个.为什么编译器仍然使用32位呢?

c c++

5
推荐指数
1
解决办法
526
查看次数

使用表达式参数专门化模板

我有一个这样的课:

template <class T>
class Foo;
Run Code Online (Sandbox Code Playgroud)

我想要专业化

template <>
class Foo < size_t N >;
Run Code Online (Sandbox Code Playgroud)

但那并不适合我:

我的主要是:

Foo<int> p;  // OK
Foo<15> p2;  // fails to compile
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

c++ templates

3
推荐指数
1
解决办法
427
查看次数

模板函数不会编译

这是我的模板功能

template<typename T> std::stringstream logging_expansion ( T const * value ){
    std::stringstream retval;
    retval = retval << *value;
    return retval;
}
Run Code Online (Sandbox Code Playgroud)

以下是我如何称它使用它

logging_expansion( "This is the log comes from the convinient function");
Run Code Online (Sandbox Code Playgroud)

但链接器告诉我它无法引用该函数:

Undefined symbols for architecture x86_64:
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >     logging_expansion<char>(char const*)", referenced from:
  _main in WirelessAutomationDeviceXpcService.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

c++ templates

1
推荐指数
1
解决办法
713
查看次数