C++ 在列表向量中的单个/多个列表中添加元素

C++ 在列表向量中的单个/多个列表中添加元素,c++,list,vector,stl,C++,List,Vector,Stl,我已经声明了一个列表向量和3个列表,并将这3个列表添加到向量中。现在,我想通过仅使用向量向3个列表中的2个添加一些元素。我如何才能做到这一点? 以下是我目前的代码: #include<iostream> #include<list> #include<vector> using namespace std; #ifndef ILIST #define ILIST list<int> #endif #ifndef VLIST #define V

我已经声明了一个列表向量和3个列表,并将这3个列表添加到向量中。现在,我想通过仅使用向量向3个列表中的2个添加一些元素。我如何才能做到这一点?
以下是我目前的代码:

#include<iostream>
#include<list>
#include<vector>
using namespace std;


#ifndef ILIST
#define ILIST list<int>
#endif

#ifndef VLIST
#define VLIST vector<ILIST >
#endif

int main(int argc, char const *argv[])
{
    ILIST l1(4,10), l2(4,20), l3(4,30);
    VLIST vec;
    vec.push_back(l1);
    vec.push_back(l2);
    vec.push_back(l3);
    //here I want to add 2 elements in l2 and 1 in l3 by using the vector only. 
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
#ifndef ILIST
#定义ILIST列表
#恩迪夫
#ifndef列表
#定义VLIST向量
#恩迪夫
int main(int argc,char const*argv[]
{
ILIST l1(4,10)、l2(4,20)、l3(4,30);
VLIST-vec;
向量推回(l1);
向量推回(l2);
向量推回(l3);
//这里我只想使用向量在l2中添加2个元素,在l3中添加1个元素。
返回0;
}

快速了解当前代码的功能:

ILIST l1(4,10), l2(4,20), l3(4,30);
三个局部变量

VLIST vec;
局部向量

vec.push_back(l1);
向量现在分配一些动态内存来存储至少一个ILIST,然后将l1的内容复制到该内存中。这两个国家现在是独立的

如果您希望有一个基本上是视图的向量,允许您通过它操纵目标对象,则需要在向量中存储指针或引用:

#include <iostream>
#include <list>
#include <vector>

using ilist_t = std::list<int>;
using vecilist_t = std::vector<ilist_t*>;

int main()
{
    ilist_t il;  // empty list
    vecilist_t vec;  // empty vector

    vec.push_back(&il);  // store address in vec[0]

    vec[0]->push_back(42);  // vec[0] has type `ilist_t*`, -> dereferences

    for (int i : il) {
        std::cout << i << '\n';
    }
}
f
返回的向量的地址为
i
,该地址具有自动存储持续时间-其生存期在函数作用域的末尾结束,使返回对象中指向它的指针无效,并且随后会发生未定义的行为

---编辑---

不清楚为什么需要独立列表。如果只需要3个列表的向量,可以执行以下操作:

#include <vector>
#include <list>

using ilist_t = std::list<int>;
using ilvec_t = std::vector<ilist_t>;

int main() {
    ilvec_t ilvec;
    ilvec.resize(3);  // now contains 3 empty lists.

    // push a value onto the 2nd list
    ilvec[1].push_back(42);
}
#包括
#包括
使用ilist\u t=std::list;
使用ilvec_t=std::vector;
int main(){
ilvec_t ilvec;
resize(3);//现在包含3个空列表。
//将值推送到第二个列表中
ilvec[1]。推回(42);
}
如果向量的编译时大小是固定的,那么可以改用std::array

using ilarray_t = std::array<ilist_t, 5>;  // compile time size fixed at 5.
使用ilarray\u t=std::array;//编译时大小固定为5。

快速了解当前代码的功能:

ILIST l1(4,10), l2(4,20), l3(4,30);
三个局部变量

VLIST vec;
局部向量

vec.push_back(l1);
向量现在分配一些动态内存来存储至少一个ILIST,然后将l1的内容复制到该内存中。这两个国家现在是独立的

如果您希望有一个基本上是视图的向量,允许您通过它操纵目标对象,则需要在向量中存储指针或引用:

#include <iostream>
#include <list>
#include <vector>

using ilist_t = std::list<int>;
using vecilist_t = std::vector<ilist_t*>;

int main()
{
    ilist_t il;  // empty list
    vecilist_t vec;  // empty vector

    vec.push_back(&il);  // store address in vec[0]

    vec[0]->push_back(42);  // vec[0] has type `ilist_t*`, -> dereferences

    for (int i : il) {
        std::cout << i << '\n';
    }
}
f
返回的向量的地址为
i
,该地址具有自动存储持续时间-其生存期在函数作用域的末尾结束,使返回对象中指向它的指针无效,并且随后会发生未定义的行为

---编辑---

不清楚为什么需要独立列表。如果只需要3个列表的向量,可以执行以下操作:

#include <vector>
#include <list>

using ilist_t = std::list<int>;
using ilvec_t = std::vector<ilist_t>;

int main() {
    ilvec_t ilvec;
    ilvec.resize(3);  // now contains 3 empty lists.

    // push a value onto the 2nd list
    ilvec[1].push_back(42);
}
#包括
#包括
使用ilist\u t=std::list;
使用ilvec_t=std::vector;
int main(){
ilvec_t ilvec;
resize(3);//现在包含3个空列表。
//将值推送到第二个列表中
ilvec[1]。推回(42);
}
如果向量的编译时大小是固定的,那么可以改用std::array

using ilarray_t = std::array<ilist_t, 5>;  // compile time size fixed at 5.
使用ilarray\u t=std::array;//编译时大小固定为5。

为什么要编写3行代码来定义类型别名,而不是使用ILIST=std::list?另外,
vec.push_back(x)
会在
vec
的后面创建一个新的
x
,因此您必须将
vec
存储的内容更改为指针或
std::reference_wrapper
@kfsone我不知道第一种方法,我还不熟悉C/CPP的所有概念。由于Java性能差,最近搬到了这里。我想你的第二个评论可以保持我的答案,对吧?也许你最好稍微解释一下为什么你想要同时拥有列表的两个视图——答案的实现需要稍微多跳一点到C++的稍微更深的一端(指针可以是学习C++的一个主要的痛点,而RealthyPoxC包装是C++ 11,新的语言,特性)。@kfsone我想根据动态提供的玩家数量,将52张牌分成一组相等的玩家。因此,向量将保存我可以遍历的每个玩家的卡片列表?另外,
vec.push_back(x)
会在
vec
的后面创建一个新的
x
,因此您必须将
vec
存储的内容更改为指针或
std::reference_wrapper
@kfsone我不知道第一种方法,我还不熟悉C/CPP的所有概念。由于Java性能差,最近搬到了这里。我想你的第二个评论可以保持我的答案,对吧?也许你最好稍微解释一下为什么你想要同时拥有列表的两个视图——答案的实现需要稍微多跳一点到C++的稍微更深的一端(指针可以是学习C++的一个主要的痛点,而RealthyPoxC包装是C++ 11,新的语言,特性)。@kfsone我想根据动态提供的玩家数量,将52张牌分成一组相等的玩家。因此向量将保存我可以遍历的每个玩家的牌列表。
for(inti:il){std::cout Nevermind,我明白了。代码的第二部分,你能给我一些建议来解决这个问题吗?@AkashAggarwal我想知道你到底需要独立列表还是只需要一个列表向量。要通过向量向
l2
添加一个元素,请使用
vec[1]>push_back(值)
。这就是你所说的代码的第二部分吗?代码的第二部分是第二个片段,如果我不清楚的话,很抱歉。我遇到了你指定的问题,因为我在for循环中创建了
ilist
,并将其地址推到
vl