小编JeJ*_*eJo的帖子

如何更新 std::vector<pair<int, int>> 中最近输入的元素?

如何更新任何对类型向量类中对的值?

例子 :

V.push_back(make_pair(1, 3));
Run Code Online (Sandbox Code Playgroud)

如果我想更新3某件事5或某事,我该如何实现?

c++ stdvector c++11 std-pair c++17

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

我可以在每行中使用具有不同列数的数组吗?

我想使用一个数组来存储图的邻接表。其中每个节点具有连接到其的不同数量的节点。所以我只想拥有以下类型的数组:

Row 0: 1 5 3
Row 1: 0 2 3
Row 2: 1
Row 3: 0 1 5 4
Row 4: 3 5
Row 5: 0 1 3 4 
Run Code Online (Sandbox Code Playgroud)

c++ algorithm graph multidimensional-array data-structures

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

为什么我的析构函数仅被调用一次,而不在delete(a)调用上被调用?

using namespace std;

class A {};

class B {};

class C : public A {
    public:
    C(){ b = new B();}
    B* b;
    ~C(){
        printf("in destructor\n");
        delete b;
    }
};

void doSomething(A&aref)
{
    C c = (C &) aref;
    c.b = new B();
    int i;
}

int main()
{      
    A * a;

    a = new C();
    printf("Line 1\n");
    doSomething(*a);
    printf("Line 2\n");

    delete(a);

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

输出为:

Line 1
in destructor
Line 2
Run Code Online (Sandbox Code Playgroud)

尝试删除delete(a)并获得相同的结果。

为什么我看不到析构函数两次调用?我希望可以在doSomething函数末尾和delete调用时调用它。 …

c++ oop inheritance destructor class

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

如何实现可变参数模式以将可变数量的参数转发给 C++11 中的函数?

在 python 中,我可以使用*args允许可变数量的输入到函数中。例如,以下代码段将打印出调用 f 时传递的所有参数:

def f(*args):
  for a in args:
    print(a)
Run Code Online (Sandbox Code Playgroud)

我希望能够在具有以下要求的 C++11 中实现这样的模式:

函数 f 将始终接受特定类型 T 的值,然后是可变数量的输入;这可能包括 0 个额外的输入。

额外的输入不一定是相同的类型,所以使用初始化列表是行不通的。

函数 f 将被另一个函数 g 调用,该函数需要将可选参数转发给 f:

T g(const T& x, args...) {
  T output = f(x, args...);
  return output;
};

T f(const T& x, args...) {
  // do some stuff and return an object of type T
};
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个设计问题?我尝试过可变参数模板,但似乎无法使我的实现正常工作(编译但由于右值引用问题而无法链接)。

c++ templates variadic-functions c++11

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

How to print std::map<int, std::vector<int>>?

Following is my code for creating a map<int, vector<int>> and printing:

//map<int, vector>
map<int, vector<int>> int_vector;
vector<int> vec;
vec.push_back(2);
vec.push_back(5);
vec.push_back(7);

int_vector.insert(make_pair(1, vec));

vec.clear();
if (!vec.empty())
{
    cout << "error:";
    return -1;
}
vec.push_back(1);
vec.push_back(3);
vec.push_back(6);
int_vector.insert(make_pair(2, vec));

//print the map
map<int, vector<int>>::iterator itr;
cout << "\n The map int_vector is: \n";
for (itr2 = int_vector.begin(); itr != int_vector.end(); ++itr)
{
    cout << "\t " << itr->first << "\t" << itr->second << "\n";
}
cout << endl;
Run Code Online (Sandbox Code Playgroud)

The printing part …

c++ iterator stdmap stdvector c++17

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

向向量的所有元素添加一个整数

我有一个整数向量,可以说{1, 2, 3, 4}

如何10为每个元素添加常量值以将向量修改为{11, 12, 13, 14}

如果我想将每个元素都除以an int并修改向量,则与除法相同。我一直找不到解决方案。

c++ algorithm add stdvector c++11

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

A variable (or an attribute) can be equal to a type?

I wanted to know if a variable can be equal to a type (here it's the magic_type)

#include <typeinfo>

template<typename T>
class C
{
public:
   magic_type t;
   t list;
   T data;

   C(void)
   {
      if (typeid(data) == typeid(int))
         t = float; // so typeid(list) = typedid(float)
      else
         t = int; // and typeid(list) = typedid(int)
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates types class-template c++17

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

大写字母

我有一个小问题。我想利用字符串中的双字母大写。我设法编译了一个程序,但没有成功。

#include <iostream>
#include <cctype>
#include <string>

std::string::iterator function(
   std::string::const_iterator a, 
   std::string::const_iterator b,
   std::string::const_iterator e)
{
   for (; a < b; a++) 
   {
      if (*a == *(a + 1)) 
      {
         toupper(*a);
         toupper(*(a + 1));
      }
   }
}

int main()
{
   std::string in = "peppermint 1001 bubbles balloon gum", out(100, '*');
   auto e = function(in.cbegin(), in.cend(), out.begin());

   int n = e - out.begin();
   std::string s = out.substr(0, n);
   bool b = (s == "pePPermint 1001 buBBles baLLOOn gum");
   std::cout << std::boolalpha …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm iterator loops stdstring

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

可变参数宏,用于检查变量是否等于可变参数之一

开发一个类似 WebGL 的 OpenGL 包装器,其中涉及验证参数实际上是否有效。问题或多或少是 OpenGL 有大量(我在这里并没有夸大其辞)某些函数的有效参数。 (一个朴素条件的图像(如果你感兴趣的话,可能是 的internalformat常量。我实际上为其中的一些编写了一个 JS 表解析器/条件生成器)来表达我的观点。那是......)glTexImage2Dassert

这种检查的逻辑很简单:

bool check(const unsigned int var)
{
    return var == CONSTANT_1 || var == CONSTANT_2 || var == CONSTANT_...;
}
Run Code Online (Sandbox Code Playgroud)

但是,根据上面的示例,这可以扩展到最多 84 个可能的常量,这是......是的。我完全意识到宏不会将其减少到荒谬的程度,但它仍然会产生影响,而且我觉得它也会更干净。更不用说具有不同运算符的类似方式的其他宏的可能性。

所以我的想法是使用宏。毕竟,使用std::initializer_list``, std::vector , std::array` 或其他容器在运行时进行此检查是微不足道的,但由于所有这些常量在编译时都是已知的,所以我觉得这是不必要的。

由于可能的常量的数量是可变的,我认为没有办法不使用可变参数宏。但我不知道如何实现这一目标。我想到的一种可能的方法是根据参数的数量重载宏(类似于 this),但这似乎不必要地复杂。

本质上,我正在寻找一个能够满足以下条件的宏:

MACRO(var, CONSTANT_1, CONSTANT_2, CONSTANT_3)
Run Code Online (Sandbox Code Playgroud)

扩展到

var == CONSTANT_1 || var == CONSTANT_2 || var == CONSTANT_3
Run Code Online (Sandbox Code Playgroud)

具有任意数量的常量(重要!也是困难的部分)。

c++ algorithm macros templates c++17

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

下面使用“typename”有什么区别?

typename在函数的返回类型之前使用“”与在函数声明中不使用它(如下所示)有什么区别?

如果我们根本不使用它,会有什么不同呢?

template< class T > typename std::remove_reference<T>::type&& move( T&& t );
template< class T > std::remove_reference_t<T>&& move( T&& t ) ;
Run Code Online (Sandbox Code Playgroud)

c++ templates return-type typename function-templates

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