小编JeJ*_*eJo的帖子

是否可以在迭代期间在数组中添加元素?

我想对元素进行排序。

例如:最初,我获取5元素并对其进行排序。问题是,下一个5元素应添加到先前的5元素中,然后再执行排序。

在向量中执行的任何内置函数吗?

c++ sorting algorithm function stdvector

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

`std :: swap`不能按预期的那样在字符串处理中工作

我试图通过交换两个连续的字母来进行基本的字符串加密。而且它并没有真正按照我的预期工作。

#include <iostream>
#include <string.h>
#include <algorithm>

int main() 
{
    std::string str = "This is a simple string.";
    for (int i = 0; i <= str.length(); i++) {
        std::swap(str[i], str[i + 1]);
    }
    std::cout << str;
    std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

我实际上想交换两个附近的字母,所以它看起来像加密的。当前结果是

his is a simple string.
Run Code Online (Sandbox Code Playgroud)

c++ encryption algorithm loops stdstring

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

如何使用初始化列表初始化inordered_map &lt;string,unordered_set &lt;string &gt;&gt;成员?

我有这堂课

class A {
    unordered_map<string, unordered_set<string>> n_;
  public:
    A(unordered_map<string, unordered_set<string>>& n) : n_{n} {}
};
Run Code Online (Sandbox Code Playgroud)

我希望能够将构造函数与该语法一起使用

int main() {
    A a{{"C", {"A", "B"}}};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是以现在的方式写的,我出错了

error: no matching function for call to `‘A::A(<brace-enclosed initializer list>)’ A a{{"C", {"A", "B"}}};`
Run Code Online (Sandbox Code Playgroud)

如何解决?

c++ unordered-map initializer-list unordered-set c++11

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

使用和不使用 new 关键字创建 C++ 对象

使用 new 关键字创建对象:

#include <iostream>
#include <string>

using namespace std;

class Person {
  private:
  string name;

  public:
  Person(string name) {
    setName(name);
  }

  string getName() {
    return this->name;
  }

  void setName(string name) {
    this->name = name;
  }
};

int main() {
  Person *person1 = new Person("Rajat");
  Person *person2 = person1;

  person2->setName("Karan");

  cout << person1->getName() << endl;
  cout << person2->getName() << endl;

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

输出:

Karan  
Karan
Run Code Online (Sandbox Code Playgroud)

创建没有 new 关键字的对象:

#include <iostream>
#include <string>

using namespace std;

class Person {
  private: …
Run Code Online (Sandbox Code Playgroud)

c++ class copy-constructor dynamic-memory-allocation c++17

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

智能指针的多态性

正如答案所指出的,这是我犯的一个愚蠢的错误,与多态性或智能指针无关。更正后的版本在接受的答案中。

==============原始问题==================

我正在尝试使智能指针与多态一起工作。在下面的原型代码中,纯virtual函数的实现Base::print()应该在Derived对象的内存块中。DerivedWrap可以访问指向Derived对象的指针。

为什么不能DerivedWrap::print()访问函数实现?

using namespace std;

class Base 
{
public:
    virtual void print() = 0;
};

class Derived : public Base 
{
public:
    Derived(int in) : i(in) {}

    void print() {
        cout << "int is " << i << endl;
    }

private:
    int i;
};

class DerivedWrap 
{
public:
    DerivedWrap() : DerivedWrap(make_unique<Derived>(2)) {}
    DerivedWrap(unique_ptr<Base> pBase) : _pBase(move(pBase)) {}

    void print()
    {
        _pBase->print();
    }

private:
    unique_ptr<Base> …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism class smart-pointers c++11

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

如何强制,值成为左值引用?

我希望我的函数采用左值引用,而绝对不是右值临时或其他任何

这是我的功能:

template<class T>
void foo(T& value) {}

// Imagine I have a class Foo
struct Foo
{ 
   int a;
   int b;
};
Run Code Online (Sandbox Code Playgroud)

当我调用 时foo(Foo{1, 2}),首先,即使我要求左值引用,它也会编译,其次,它不起作用,因为foo存储了传递值的地址,所以我稍后阅读时会得到垃圾。

如何强制foo采用左值引用?

c++ templates lvalue function-templates c++11

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

在声明中合并两个常量 `std::set`s(不是在运行时)

我试图优雅地声明一个常量std::set对象,它将是另外两个常量std::set对象的合并。

#include <set>

const std::set<int> set_one = { 1,2,3 };
const std::set<int> set_two = { 11,15 };
const std::set<int> set_all = { 1,2,3,11,15 }; // this is not very elegant, duplication
Run Code Online (Sandbox Code Playgroud)

set_all这种方式声明对象不太优雅,因为它复制了前两行的信息。有没有办法在声明中使用set_oneset_two常量set_all

像这样的东西:

const std::set<int> set_all = set_one + set_two; // this does not compile, of course!
Run Code Online (Sandbox Code Playgroud)
  1. 所有对象都是严格的常量。
  2. 两个源集中没有重叠值,因此唯一性不会成为问题。
  3. 我知道如何在运行时合并集合,这不是我要找的。
  4. 我真的很想避免使用这样的宏:
#include <set>

#define SET_ONE 1, 2, 3
#define SET_TWO 11, 15

const std::set<int> set_one = { SET_ONE …
Run Code Online (Sandbox Code Playgroud)

c++ c++-standard-library stdset data-structures c++11

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

如何在if语句中定义变量和比较值?

我有一个这样的代码片段:

if ((std::vector<int>::iterator iter = std::find(v.begin(), v.end(), i)) != v.end()) 
{
    // ....
}
Run Code Online (Sandbox Code Playgroud)

但是编译器抱怨这个语句。但是,将我的代码更改为

std::vector<int>::iterator iter;
if ((iter = std::find(v.begin(), v.end(), i)) != v.end()) 
{
    // ....
}
Run Code Online (Sandbox Code Playgroud)

解决了这个问题。

所以,我想知道为什么第一个版本不起作用,下面语句的返回值是什么?

std::vector<int>::iterator iter = std::find(v.begin(), v.end(), i)
Run Code Online (Sandbox Code Playgroud)

c++ variables syntax if-statement initialization

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

为什么在模板函数中不允许使用 auto 来创建内置数组?

下面的代码无法编译:

template<typename... Ts>
void CreateArr(const Ts&... args)
{
    auto arr[sizeof...(args) + 1]{ args... };
}

int main()
{
    CreateArr(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)

由于以下错误:

  • 'arr':在直接列表初始化上下文中, for 的类型'auto [6]'只能从单个初始化表达式推导
  • auto [6]':数组的元素类型不能包含'auto'
  • “正在初始化”:无法从 转换'const int''std::initializer_list<int>'

我的问题是:

  • 为什么我不能使用auto来定义数组的类型?

  • 如何正确定义它以与模板一起使用?

c++ templates auto c++11 c++17

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

使用 if constexpr 时扣除模板参数失败

我试图弄清楚 sfinae 概念在 C++ 中是如何工作的。但我无法说服对象类型编译器 if boolis trueor false

#include <iostream>

class A {
public:
    void foo() { std::cout << "a\n"; }
};

class B {
public:
    void ioo() { std::cout << "b\n"; }
};

template<class T>
void f(const T& ob, bool value = false)
{
    if constexpr (!value) ob.foo();
    else  ob.ioo();
}

int main()
{
    A ob1;
    B ob2;
    f(ob1, true);
    f(ob2, false);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae function-templates c++17

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