小编dee*_*ive的帖子

哪种算法更快O(N)或O(2N)?

谈论Big O符号,如果一个算法时间复杂度为O(N)而其他算法为O(2N),哪一个更快?

algorithm big-o

27
推荐指数
4
解决办法
3万
查看次数

Gitlab有条件地执行阶段

有3个阶段 - 构建,测试和部署.gitlab-ci.yml.

需要运行夜间回归测试阶段 - 好nightly:)

这是相关的.gitlab-ci.yml代码:

stages:
  - build
  - test
  - deploy

build_project:
  stage: build
  script:
    - cd ./some-dir
    - build-script.sh
  except:
  - tags

#Run this only when say variable 'NIGHTLY_TEST == True'. But HOW?
nightly_regression_test_project:
  stage: test
  script:
    - cd ./some-dir
    - execute test-script
Run Code Online (Sandbox Code Playgroud)

每天标记到only运行test阶段是不可取的.

还有其他想法吗?

git gitlab gitlab-ci

22
推荐指数
4
解决办法
1万
查看次数

这是构造函数运算符还是转换运算符?

class B
{
    public:
        operator B() const{ }    // What is this and what is the purpose?

    private:
        int m_i;
};
Run Code Online (Sandbox Code Playgroud)

所以问题是,转换运算符或构造函数运算符是什么用的呢?在哪里使用它?

c++

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

为什么重载'operator <'应该是类的const?

任何人都可以在STL sort算法的背景下解释这种行为吗?如果operator <没有定义const它会给出错误,

error: passing ‘const B’ as ‘this’ argument of ‘bool B::operator<(const B&)’ discards qualifiers [-fpermissive] while (__pivot < *__last)

sort算法中LHS const对象,或者sortconst方法?

class B
{
 public:
    ...
    bool operator < (const B& b) const       // why const required here?
    {
        return (m_i < b.m_i);
    } 
    ...

 private:
    int m_i;
    int m_j;
};

int main()
{
  vector<B> Bvec2 {B(5), B(3), B(30), B(20), B(8)};
  std::sort(Bvec2.begin(), Bvec2.end());
  ...
}
Run Code Online (Sandbox Code Playgroud)

c++ stl operator-overloading

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

具有多个线程和互斥锁的竞争条件

int iGlobe = 0;
...
void thread1Func()
{
  Lock(&Mutex1);
  if(iGlobe == 0)         //step-1
    someaction();
}
...
void thread2Func()
{
  Lock(&Mutex2);
  iGlobe = 5;             //step-2
}
Run Code Online (Sandbox Code Playgroud)

假设,

1)Thread1执行步骤1并进入休眠状态

2)同时Thread2执行步骤2并改变iGlobe的值

如何克服这种情况?

c++ multithreading

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

为什么要为重载的运算符 new 调用类构造函数?

我正在使用 c++11 (g++ v4.7.2)

我已经为“基”类重载了运算符 new 和运算符删除。显然,他们不应该在调用 new/delete 时调用构造函数/析构函数,因为我没有在重载的 new/delete 中实现 ctor/dtor 调用。但输出却与此相反

//Output of below program
Base new opnd.cpp 87
Base ctor
10
Base dtor
Base delete
Run Code Online (Sandbox Code Playgroud)

为什么重载运算符 new/delete 会调用 ctor/dtor?

#include <iostream>

using namespace std;

#define NEW new(__FILE__, __LINE__)
#define DELETE delete

class Base
{
    public:
        Base():m_i(10){ cout << "Base ctor" << endl; }
        virtual ~Base(){ cout << "Base dtor" << endl; }

        void* operator new(size_t size, const char* file, int line) throw(std::bad_alloc);
        void operator delete(void *rawMem, size_t …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

printf vs cout表现

void perfprint(unsigned int count)
{
    char a[100] = "fosjkdfjlsjdflw0304802";
    for(unsigned int i = 0;i<count;++i)
    {
        printf("%s", a);
    }
}

void perfcout(unsigned int count)
{
    char a[100] = "fosjkdfjlsjdflw0304802";
    for(unsigned int i = 0;i<count;++i)
    {
        cout << a;
    }
}
Run Code Online (Sandbox Code Playgroud)

环境:C++,VS 2010,Windows 7,32位,Core-i7,4GB,3.40 GHz

我测试了与功能count = 100005次,每次.使用测量性能QueryPerformanceCounter.

perfprint> perfprint毫秒(5次运行的平均值)

perfcout> ~850毫秒(5次运行的平均值)

这是否意味着printf perfcout比cout更快

编辑:

使用/ Ox,/ Ot,Release版本中没有调试信息

并用~9000在perfcout方法,结果是为相同printf,即~10x

编辑2:

总而言之,cout比快std::ios_base::sync_with_stdio(false);.上述观察的原因是由于控制台输出.当将输出重定向到文件时,事情开始转向!

c++ windows

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

初始化char*没有给出错误

struct st{ 
int a; 
char *ptr; 
}obj; 

main()
{   
   obj.a=10;    
   obj.ptr="Hello World";          // (1) memory allocation?
   printf("%d,%s",obj.a,obj.ptr);
}
Run Code Online (Sandbox Code Playgroud)

ptr在struct中声明.Hello world发生分配时,未分配内存,但此程序正常工作并正确输出.在标记处完成作业时,它不应该失败/崩溃(1)吗?

c c++

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