小编wai*_*r92的帖子

使用remove_if从向量中删除元素

我试图删除矢量元素使用remove_if.但没有成功.我究竟做错了什么?

这是我的代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void printme(std::vector<int>& a){
    for(const auto& item: a)
    std::cout << item << std::endl;
}

int main()
{
    std::vector<int> a {1, 2, 3, 4, 5, 6};
    printme(a);  
    a.erase( (std::remove_if(a.begin(), a.end(), [](const int& x){
        return x == 2;
        }), a.end()));
    printme(a);
}
Run Code Online (Sandbox Code Playgroud)

我的输出只是:

1 2 3 4 5 6

预期产量:

1 2 3 4 5 6 1 3 4 5 6

c++ stl vector c++11

11
推荐指数
4
解决办法
1535
查看次数

在C++ 11函数中使用尾部返回类型的优点

与普通返回类型相比,在C++ 11中指定尾随返回类型有什么好处?看看foo1vs foo2在这里:

int foo1() {
    return 1;    
}

auto foo2() -> int {
    return 1;    
}

int main() {
    foo1();
    foo2();
}
Run Code Online (Sandbox Code Playgroud)

c++ auto c++11 trailing-return-type

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

对容器进行多次排序,使用什么容器和什么方法

我有一些我需要打印的数据,为简单起见,我们说这是一个有一些参数的人的容器(矢量).在我的程序的不同部分,我需要打印所有按不同参数排序的部分.我的问题是

1.)选择哪个容器?(我个人选择了矢量).

2.)什么方法更好,每次都对整个矢量进行排序,或者最好制作该矢量的副本并保存它?在我的解决方案中,我每次都对相同的矢量进行排序,但由于速度的原因,可能不是一个使用大型矢量的正确方法.

class Person
{
private:
    std::string name;
    std::string surname;
    int age;
public:
    Person(std::string name, std::string surname, int age) : name{ name }, surname{ surname }, age{ age } {};
    void print() { std::cout << name << " " << surname << " " << age << std::endl; };
    static bool sortName(Person const &A, Person const &B) { return A.name < B.name; };
    static bool sortSurname(Person const &A, Person const &B) { return A.surname < B.surname; };
    static bool …
Run Code Online (Sandbox Code Playgroud)

c++ sorting containers vector

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

在make_unique中使用此*

我有一个Factory设计模式的小例子,我对这部分感兴趣:

std::make_unique< A >(*this)
Run Code Online (Sandbox Code Playgroud)

......尤其如此*this.

这是否意味着该clone()方法返回一个std::unique_ptr指向工厂类成员的?并且createInstance()总是返回同一个Factory类的成员?

我只是混淆std::make_unique< A >(*this)应该做什么,因为A在构造函数中std::string,而不是指向自身的指针.

class Base {
    public:
        virtual ~Base() {}
        virtual std::unique_ptr<Base> clone() = 0;
        virtual void print() = 0;
};

class A: public Base {
        std::string name_;
    public:
        A(std::string name ){name_ = name;};
        std::unique_ptr<Base> clone() override{
            return std::make_unique<A>(*this);
        };
        void print( ) override{
            std::cout << "Class A: " << name_;    
        };
        virtual ~A(){};
};

class …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers factory-pattern unique-ptr c++11

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

通过引用将引用传递给lambda

有什么区别f1f2

void foo(const std::string& test){

    auto f1 = [&test](){
        std::cout << test << std::endl;
    };

    f1();

    auto f2 = [test](){
        std::cout << test << std::endl;
    };

    f2();
}

int main()
{
    std::string x = "x";
    foo(x);
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,testlambda内的变量类型均为std::string const&,但这是否真的一样?

c++ lambda reference

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

防止在大型结构之间进行不必要的复制

我有巨大的结构 DataFrom 和 Data (实际上有不同的成员)。数据是从 DataFrom 创建的。

struct DataFrom{
    int a = 1;
    int b = 2;
};
static DataFrom dataFrom;   
struct Data{
    int a;
    int b;
};    
class DataHandler{
    public:
        static Data getData(const DataFrom& data2){
            Data data;
            setA(data, data2);
            setB(data, data2);
            return data;
        }
    private:
        static void setA(Data& dest, const DataFrom& source){
            dest.a = source.a;
        }
        static void setB(Data& dest, const DataFrom& source){
            dest.b = source.b;
        }
};
int main(){
    auto data = DataHandler2::getData(dataFrom); // copy of whole Data …
Run Code Online (Sandbox Code Playgroud)

c++ copy-elision c++17

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

C++中的多态(意外行为)

我有以下三个课程:

class A
{
private:
    std::string device;
public:
    std::string getDeviceType() { return device; };
    void setDeviceType(std::string device) { device = device; };
    virtual void doSomething() = 0;
    virtual void doSomething2() = 0;
};

class B: public A
{
    private:
    public:
        B(){ ; };
        virtual ~B(){ ; };
        void doSomething() { std::cout << "I am usual B" << std::endl; };
        void virtual doSomething2() { std::cout << "I am usual B" << std::endl; };
};

class C : public B
{
private: …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance virtual-functions

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

Lambda在c ++中内部这个

如果lambda函数ic C++是由functor实现的,为什么这不可能?

#include <iostream>

class A
{
public: 
    int a;  
    void f1(){ [](){std::cout << this << std::endl ;}();};
};

int main()
{
    A a;
    a.f1();
}
Run Code Online (Sandbox Code Playgroud)

我收到错误9:34: error: 'this' was not captured for this lambda function.如果我的理解是正确的,如果拉姆达为函数子类实现的,为什么它是不可能得到它单曲内部这个

编辑:函子类,而不是这个类A的实例

c++ lambda this c++11

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

与来自 C++ 代码的 qml 对象交互

我正在尝试使用QtQuickC++文件中的qml对象进行交互。但遗憾的是暂时没有成功。知道我做错了什么吗?我尝试了两种方法,第一种方法的结果是findChild()返回了nullptr,第二次尝试时我收到Qml 组件未准备好错误。什么是正确的方法呢?

主要的:

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    // 1-st attempt how to do it - Nothing Found
    QObject *object = engine.rootObjects()[0];
    QObject *mrect = object->findChild<QObject*>("mrect");
    if (mrect)
        qDebug("found");
    else
        qDebug("Nothing found");
    //2-nd attempt - QQmlComponent: Component is not ready
    QQmlComponent component(&engine, "Page1Form.ui.qml");
    QObject *object2 = component.create();
    qDebug() << "Property value:" << QQmlProperty::read(object, "mwidth").toInt();

    return app.exec(); …
Run Code Online (Sandbox Code Playgroud)

c++ qt qml qtquick2

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