标签: std

那么将emplace(c.end(),_ 1)与emplace_back(_1)区分开来的是什么?

我认为使用emplace(c.end(),_ 1)将与emplace_back(_1)相同但我不明白为什么语言设计者给出了两个函数而不是一个函数.我假设我错过了一些信息.

那么将emplace(c.end(),_ 1)与emplace_back(_1)区分开来的是什么?

c++ std c++11

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

缺少unique_ptr和friends operator = overload

我知道这种问题在这里并不受欢迎,但我必须要问:为什么unique_ptr/shared_ptr/etc没有类型为T的operator =重载?

写作似乎更自然

std::unique_ptr<int> p = new int(5);
Run Code Online (Sandbox Code Playgroud)

代替

std::unique_ptr<int> p(new int(5));
Run Code Online (Sandbox Code Playgroud)

或任何其他更冗长的方法.

c++ std c++11

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

std :: sort functor一行

我已经声明了一个仿函数并且使用该仿函数作为参数进行了一次调用std :: sort.码:

struct
{
    bool operator() (const CString& item1, const CString& item2){
        return MyClass::Compare( Order(_T("DESC")), item1, item2);
    }

}Comparer;

std::sort(AllObjects.GetData(), AllObjects.GetData() + AllObjects.GetSize(), Comparer);
Run Code Online (Sandbox Code Playgroud)

简单的问题:我可以在一行中做到这一点吗?

c++ sorting std line functor

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

创建is_primitive或is_inheritable模板

我想创建一个模板来检查类类型是否是原始类型(int,char,float,float***,ect ...).这样做的原因是为了防止另一个模板尝试扩展原语并导致编译时错误.到目前为止,我有以下几点:

#include <typeinfo>
template<typename T>
struct is_primitive{
   const static bool value=std::is_fundamental<T>::value;
};
Run Code Online (Sandbox Code Playgroud)

显然,这只是转发is_fundamental的结果.我想添加remove_pointer,remove_reference,ect ...来删除输入类的所有额外修饰符. 使T尽可能裸露所需的所有移除是什么?

或者,像下面这样的解决方案也同样出色:

template<typename T>
struct is_inheritable{
   const static bool value=???;
};
Run Code Online (Sandbox Code Playgroud)

但我很确定不可继承的类集等于原始类的集合.

c++ templates std c++11

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

为什么std :: map <int,float>不能使用operator []:错误C2678?

有:

std::map<const int, float> m_areaCost;
Run Code Online (Sandbox Code Playgroud)

我正在尝试编译以下内容:

inline float getAreaCost(const int i) const { 
    return m_areaCost[i]; 
}
Run Code Online (Sandbox Code Playgroud)

这导致以下错误:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion) 
Run Code Online (Sandbox Code Playgroud)

我常想,当我们要求[elementId]我们获得元素值或默认的元素值,所以我不知道怎么能这么简单的情况下,会导致编译错误?

c++ stl std

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

std :: vector <Class*>无法正确构造副本

我有这样的Class粒子,

class Particle
{
public:
    std::vector<Particle*> getNbh () const;
    void setNbh (const std::vector<Particle*>&);
private:
    std::vector<Particle*> nbh_;
};
Run Code Online (Sandbox Code Playgroud)

并且功能Particle::setNbh(const std::vector<Particle*>&)已实现,

void Particle::setNbh (const std::vector<Particle*>& nbh)
{
    nbh_ = nbh;
}
Run Code Online (Sandbox Code Playgroud)

然后有一个非会员功能 updateNeighbors (std::vector<Particle>& particles, double cutoff)

void updateNeighbors (std::vector<Particle>& particles, double cutoff)
{
    for (auto particle : particles)
    {
        auto nbh = std::vector<Particle*>();
        for (auto other : particles)
            if (&particle != &other 
                && norm(particle.getPosition() - other.getPosition()) < cutoff)
                nbh.push_back(&other);
        particle.setNbh(nbh);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是当我用这个函数更新邻居时,nbh_成员没有正确更新,我测试它打印getNbh()每个粒子的大小.

这是正确的复制方式,std::vector<Particle*> …

c++ std stdvector c++11

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

有人可以解释ref()vs&c ++ 11以及何时使用每个?

我阅读了有关它的文档,但如果有人能用更简单的英语解释它,我会很感激.

c++ reference std c++11

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

什么是将地图的键集转换为键向量的有效方法

什么是将map的键集转换为键的向量的有效方法,目前我正在迭代我的地图并将iter.first添加到vector中,这类似于Java的KeySet api?

c++ stdmap std stdvector

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

如何在二维向量上使用std :: max_element

我四处寻找解决这个问题的办法,却无法发现任何问题.我相信这是一个快速解决方案,希望有人可以发现我的错误并告诉我.

这就是我所拥有的

#include<algorithm>
#include<vector>

void myFunction ( cont std::vector<std::vector<int> > &counts){
    std::vector<int>::iterator max_it;
    int index;
    for ( int i = 0 ; i < counts.size() ; i ++ ){
        max_it = std::max_element(counts[i].begin(), counts[i].end());
        index = std::distance(counts[i].begin(),max_it);
    }
}
Run Code Online (Sandbox Code Playgroud)

在编译上面的代码时,我得到了调用max_element的行的以下错误.

error: no match for ‘operator=’ in ‘it = std::max_element [with _ForwardIterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >](((const std::vector<int, std::allocator<int> >*)((const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)counts)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > …
Run Code Online (Sandbox Code Playgroud)

c++ iterator std max

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

尽管索引容量不足,但vector.at()仍会出现扼流圈

我正在使用Debian 7.0.0下的CodeBlocks 10.05开发一个C++应用程序.

因此我为一个向量分配空间.

std::vector<double> dpMeanUnnormalizedRef;
dpMeanUnnormalizedRef.reserve(expectedUpperLimit);
Run Code Online (Sandbox Code Playgroud)

然后我尝试添加一个值.不幸的是,我需要添加值的索引不是顺序的,所以我不能使用push.我第一次尝试添加元素时,调用如下.

dpMeanUnnormalizedRef.at(index)=doubleVar;
Run Code Online (Sandbox Code Playgroud)

此时,大小为0,容量为621,而索引的值为0.然而,此调用导致程序抛出超出范围的异常.

c++ std stdvector

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

标签 统计

c++ ×10

std ×10

c++11 ×5

stdvector ×3

functor ×1

iterator ×1

line ×1

max ×1

reference ×1

sorting ×1

stdmap ×1

stl ×1

templates ×1