相关疑难解决方法(0)

list :: size()真的是O(n)吗?

最近,我注意到有些人提到它std::list::size()具有线性复杂性.
根据一些 消息来源,这实际上是依赖于实现的,因为标准没有说复杂性必须是什么.此博客条目中
的评论说:

实际上,这取决于您使用的STL.Microsoft Visual Studio V6将size()实现为{return(_Size); 而gcc(至少在版本3.3.2和4.1.0中)将其作为{return std :: distance(begin(),end()); 第一个具有恒定速度,第二个具有o(N)速度

  1. 所以我的猜测是因为VC++人群的size()复杂程度不变,因为自VC6以来Dinkumware可能不会改变这一事实.我在那儿吗?
  2. 目前看起来像gcc什么?如果它真的是O(n),开发人员为什么选择这样做?

c++ complexity-theory big-o stl list

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

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

扩展std :: list

我需要为我的程序使用列表,并需要决定是否使用std :: vector或std :: list.向量的问题在于没有删除方法,并且列表中没有运算符[].所以我决定编写自己的类来扩展std :: list并重载[]运算符.

我的代码看起来像这样:

#include <list>

template <class T >
class myList : public std::list<T>
{
public:
T operator[](int index);
T operator[](int & index);
myList(void);
~myList(void);
};

#include "myList.h"

template<class T>
myList<T>::myList(void): std::list<T>() {}

template<class T>
myList<T>::~myList(void)
{
std::list<T>::~list();
}

template<class T>
T myList<T>::operator[](int index) {
int count = 0;
std::list<T>::iterator itr = this->begin();
while(count != index)itr++;
return *itr;    
}

template<class T>
T myList<T>::operator[](int & index) {
int count = 0;
std::list<T>::iterator itr = this->begin();
while(count …
Run Code Online (Sandbox Code Playgroud)

c++ linker templates list

6
推荐指数
5
解决办法
8876
查看次数

为什么不能将数字添加到关联容器的迭代器中?

我有一个std :: set,我想通过集合中的元素对进行迭代,所以我写了2个循环,如下所示:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=i+1;j!=mySet.end();++j)
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器告诉我,我无法向迭代器添加数字.但是我可以增加和减少它们.解决方法我发现我可以跳过第一次迭代:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    std::set<T>::iterator j=i;
    for(++j;j!=mySet.end();++j)
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能只添加一个数字为什么我必须增加?

c++ containers stl

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

下标a std :: list

我有这些typedef:

typedef pair<k2Base, list<v2Base>> shuffled_pair;
typedef list<shuffled_pair> shuffled_list;
Run Code Online (Sandbox Code Playgroud)

而这个功能:

shuffled_pair getItem(unsigned int index){
    return this->_items[index];
} 
Run Code Online (Sandbox Code Playgroud)

其中this->_items的类型是shuffled_list在typedef的声明.

我从编译器得到这个错误:

 Type 'const shuffled_list' (aka 'const list<pair<k2Base, list<v2Base> > >') does not provide a subscript operator
Run Code Online (Sandbox Code Playgroud)

但是类型基本上是列表类型,那么问题是什么?

c++

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

标签 统计

c++ ×5

list ×2

stl ×2

big-o ×1

c++11 ×1

complexity-theory ×1

containers ×1

linker ×1

std ×1

templates ×1