小编Wol*_*olf的帖子

链表中删除的时间复杂度

根据这个网站,我在理解为什么链接列表的时间复杂度为O(1)时遇到了一些麻烦.根据我的理解,如果你想删除一个元素肯定你必须遍历列表以找出元素的位置(如果它甚至存在)?从我的理解不应该是O(n)或我完全错过了什么?

linked-list data-structures

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

为什么我能在构造函数中初始化const?

正如标题所暗示的那样,我一直认为要设置常量值并且无法修改但是我注意到你能够在构造函数中更改它.正如您所看到的,我创建了一个字符串名称,稍后将其设置为我通过构造函数的任何内容,为什么这允许?

示例代码:

#include <iostream>
using namespace std;

class test{
    const string name;
public:
    test(const string &nam) : name(nam) {}

    string get_name() { return name; }
};

int main ()
{
    test a("Tom");
    cout << a.get_name();

   // const string t1;
  //  string t2 = "changing"; this causes an error
  //  t1 = &t2;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×1

data-structures ×1

linked-list ×1