将列表元素插入向量,C++获取其他不需要的值
Inserting list elements to vector, getting additional unwanted values C++
在我的C++脚本中,我想将列表中的一些元素插入到向量中(从列表的开头到特定位置"it"),然后尝试在列表顶部添加向量并保持向量的相同顺序,但我在向量中得到了不需要的其他元素。
这是我的代码:
#include <iostream>
#include <iterator>
#include <vector>
#include <list>
int main() {
std::list<int> mylist;
for (int i = 0; i < 10; i++)
mylist.push_back(i * 10);
for (std::list<int>::iterator i = mylist.begin(); i <= mylist.end(); ++i) {
std::cout << *i << ", ";
}
std::cout << std::endl;
std::advance(it, 6);
std::cout << "The 6th element in mylist is: " << *it << std::endl;
// The vector that will contain mylist elements
std::vector<int> intAdeplacer;
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
std::cout << std::endl;
std::cout << "Nombre d'éléments dans le vecteur : " << intAdeplacer.size() << std::endl;
std::cout << std::endl;
// see the content of the vector
std::cout << "Le vecteur de deplacement contient : " << std::endl;
for (std::vector<int>::const_iterator i = intAdeplacer.begin();
i <= intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
我得到这个输出:
Le vecteur de deplacement contient : 0, 10, 20, 30, 40, 134985,
134985是不需要的。.
// Insert in front of the list the values of the vector and keeping the same order of elements in the vector
for (std::vector<int>::const_iterator i = intAdeplacer.end();
i >= intAdeplacer.begin(); --i) {
mylist.push_front(*i);
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "nouvelle composition de mylist : " << std::endl;
for (std::list<int>::const_iterator j = mylist.begin(); j != mylist.end();
++j) {
std::cout << *j << ", ";
}
std::cout << std::endl;
std::cout << std::endl;
// erasing the added elements from mylist
std::list<int>::iterator debut = mylist.begin();
std::list<int>::iterator fin = mylist.end();
std::advance(fin, 6);
mylist.erase(debut, fin);
for (std::list<int>::iterator j = mylist.begin(); j <= mylist.end(); ++j) {
std::cout << *j << ", ";
}
return 0;
}
如果it
是指向列表第 6 个元素的迭代器,则以下insert()
将从begin()
(包含)插入到it
(排除)到向量中:
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
所以你只有 5 个元素,从 0 到 40。 不幸的是,您的打印循环确实包括矢量的end()
,这超出了范围。 这就是为什么你会得到这个奇怪的尾随数字。
只需将您的循环更正为:
for (auto i = intAdeplacer.begin(); i != intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
或者考虑以下替代范围语法:
for (auto &element: intAdeplacer) {
std::cout << element << ", ";
}
相关文章:
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 为什么output_editor Concept不需要output_e迭代器标记
- 在除法中不需要四舍五入
- 与C代码相比,为什么C++代码不需要"#define _POSIX_C_SOURCE 200809L"?
- 将值从另一个数组写入数组,不包括不需要的值 C++
- C++ 写入路径名中包含不需要的空字符的文件
- 是否有必要获取锁并在不需要唤醒线程时通知condition_variable?
- 使用 assimp 加载模型 - 不需要提升?
- 为什么转换函数声明不需要至少一个定义类型说明符
- 返回不需要的值的二叉搜索程序
- 当我不需要数据库中的所有值时,如何部分初始化 c++ 对象?
- C++:用户输入会产生不需要的行为
- 如何在不需要LIBCD.lib的情况下在Visual Studio 6中编译C项目
- 为什么在一个地方需要双倍英镑,而在这个宏观上不需要其他地方?
- 创建不需要任何进一步安装即可在其他计算机上工作的可执行文件?
- windeployqt错过了一些库,而获得了其他不需要的库
- 如何处理dervied类的多个重载,而不需要对其他文件进行多次更改
- 将列表元素插入向量,C++获取其他不需要的值
- 如果我只需要变量在其他线程中的值,我应该在一个线程中锁定变量吗?如果我不需要它为什么它有效?