小编Ann*_*inn的帖子

为什么std :: bitset :: at()抛出out_of_range?

这让我困扰了几个小时,因为我在数学或代码中看不到任何问题.(尽管盯着它看,并一遍又一遍地努力确定.)我希望大家可以帮助我,这是我的代码:

#define SOLVE_POSITION(x, y, z) ( z*16  +  y*4  +  x )

std::bitset<64> block;
block.reset();

for(int z = 0; z < 4; ++z){
    for(int y = 0; y < 4; ++y){
        for(int x = 0; x < 4; ++x){

            if(block.at(SOLVE_POSITION(3-x, y, 3-z))){  //<-- call to at() throws 'out_of_range'

                // do stuff
            };
        };
    };
};
Run Code Online (Sandbox Code Playgroud)

z为0时,两个最内部for循环完全运行它们(总共16次传递.)但是,一旦z变为1,那就是从std :: bitset <64> :: at()中抛出异常的时候.

的值z,y,x分别为 1,0,0在那一刻.

你能告诉我这里发生了什么导致这个例外吗?提前致谢!

c++ algorithm debugging stl exception

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

C++:编译器是否优化整数+浮点算术运算?

编译器是否有权甚至能够优化算术运算,其中一方是整数类型,而另一方是浮点数?或者,在100%的情况下执行操作之前,整数是否会被提升为浮点数?

我问的原因是因为我喜欢为了清晰起见而自己执行浮动促销,但如果它保留了编译器可以利用的情况,我可能会停止这样做.

c++ floating-point optimization integer compilation

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

Give a std::vector ownership of another vector's memory?

I have a constructor that looks like this:

Thing::Thing(std::vector<uint8> & data){
    m_data = data; // m_data is also a vector<uint8>
    // do other stuff
}
Run Code Online (Sandbox Code Playgroud)

However, data holds a pretty large chunk of memory, and instead of copying it, I'd like data to simply give it up to m_data, as the caller will never need it after constructing this object. What's the best way to do this in C++?

c++ optimization vector

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

std::deque 会保留其所包含对象的指针有效性吗?

我正在寻找一个保留其所包含对象在内存中的位置的容器(它的指针保持有效)

容器会不断增大和缩小。中间的元素可以被删除,但是中间没有插入;所有元素都被推到容器的背面。在这种情况下,迭代器的有效性并不重要,我唯一关心的是指针仍然有效。

在这种情况下是std::deque安全有效的选择吗?我以前使用过列表,但它分配的次数太多,在这种情况下没有用处。

c++ memory list visual-studio-2010 deque

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

确定 vsprintf 输出的大小

我正在printf(const char * format, ...)为一个类实现一个类似的方法,在调用执行实际写入之前,我需要确定它的输出的确切大小,仅给出提供的format和给出的参数。va_listvsprintf()

是否有一个函数可以使用formatva_list来生成输出的确切长度?

c++ printf visual-studio-2015

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

插入时同时迭代地图,以什么方式不安全

注意:我知道这是标准的不安全和未定义,看看它是否由我的编译器定义,或者它是否在实践中是安全的.

我在一个线程中迭代一个映射范围,同时可能插入另一个线程

// thread 1:
for(auto it = map.begin(); it != map.end(); ++it){
    // it's okay if "it" is out of order, repeats an element, or skips an element
    // it's bad  if "it" can skip map.end() or turn to mush (invalid iterator)
}

// thread 2:
map[Key(...)] = Type(...); // insertions are extremely rare but inevitable
Run Code Online (Sandbox Code Playgroud)

这是不安全的,但是......多么不安全?此映射用作缓解线程争用的优化提示,因此它本身无法促成该争用.如果可能的结果只是插入的元素可能会丢失,或者它会读取无序或两次的元素,这是可以接受的,并且不会破坏任何内容.

这会把迭代器变成汤还是导致map.end()错过?这是唯一可能破坏我生命的两个结果.

c++ concurrency optimization c++11 visual-studio-2015

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

if-else 语句会在没有括号的情况下嵌套吗?

我想写一些非常荒谬的东西,需要非常深的条件嵌套。写这个最不迷惑的方法是完全放弃括号,但我无法找到任何关于嵌套单语句 if-else 守卫是否合法的信息;非嵌套版本似乎给人们带来了足够的问题。

写以下内容是否有效?(在 C 和 C++ 中,如果他们对此有所不同,请告诉我。)

    float x = max(abs(min), abs(max));
    uint32 count = 0u;

    // divides and conquers but, tries to shortcut toward more common values

    if (x < 100'000.f) 
        if (x < 10.f) 
            count = 1u;
        else
            if(x < 1'000.f)
                if (x < 100.f) 
                    count = 2u;
                else
                    count = 3u;
            else 
                if (x < 10'000.f) 
                    count = 4u;
                else
                    count = 5u;
    else 
        ... // covers the IEEE-754 float32 range to ~1.0e+37 (maybe 37 end …
Run Code Online (Sandbox Code Playgroud)

c c++ if-statement

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

是否有编写移动构造函数的简写方法?

我有一个类,有一个强指针和很多对象成员。为这样的对象编写复制和移动构造函数涉及大量繁琐的复制/粘贴,但是......

有什么办法可以缩短这个时间,而又不放弃我美丽的裸指针吗?就像我是否可以执行默认生成的移动操作,然后只需要一条额外的指令来使裸指针无效?

class Example {
public:
    Example() 
        : m_multiplexDevice(getDevice(B737M_mpDevice, true))
    {
        engageDevice(m_multiplexDevice);
    }

    Example(Example && other)
        : m_multiplexDevice     (other.m_multiplexDevice    )
        , m_pilotStack          (std::move(other.m_pilotStack       ))
        , m_pasStack            (std::move(other.m_pasStack         ))
        , m_pitchAttackLimits   (std::move(other.m_pitchAttackLimits))
        , m_yawToPitchBalance   (std::move(other.m_yawToPitchBalance))
        , m_engineBalance       (std::move(other.m_engineBalance    ))
    { 
        other.m_multiplexDevice = NULL;
    }

    Example & operator=(Example && other) {
        if (this != &other) {
            // ignore that this is incorrect (not properly destroying in assignment), 
            // working with client code that kinda sucks
            m_multiplexDevice    = other.m_multiplexDevice;
            m_pilotStack         = std::move(other.m_pilotStack         ); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor move

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