向向量添加元素
Adding elements to a vector
我需要使用 mergesort 对 2 个向量(A 和 B(进行排序,并将排序后的元素放入第三个向量 (R( 中。
在 A (1,3,5( 和 B 中有 3 个元素的测试中 (2,4,6(,我的代码运行正常,直到我必须插入第 4 个元素。此时,我的代码崩溃并出现矢量下标超出范围错误。
这是我第一次使用向量,但我认为push_back()
函数会调整向量的大小。我的预感是目标向量 (R( 只能容纳 3 个元素,所以当我插入第 4 个元素时,我的代码崩溃了。我需要做一些事情来调整 R 的大小吗?
using namespace std;
#include <iostream>
#include <vector>
// combine two sorted lists A and B into R
// displays comparison every time it is done
void combine(vector<int> A, vector<int> B, vector<int>& R)
{
int ia = 1;
int ib = 1;
int ir = 1;
while (!A.empty() && !B.empty()) {
if (A[ia] < B[ib]) {
R.push_back(A[ia]);
ia++;
ir++;
}
else {
R.push_back(B[ib]);
ib++;
ir++;
}
}
if (!A.empty()) {
for (int i = ia; i < A.size(); i++) {
R.push_back(A[i]);
}
}
else if (!B.empty()) {
for (int i = ib; i < B.size(); i++) {
R.push_back(B[i]);
}
}
cout << "comparison" << endl;
// be careful -- R comes in as an empty vector
}
int main()
{
vector<int> L1;
vector<int> L2;
vector<int> L3;
int N; // how many elements in each of L1 and L2
int e; // for each element
cout << "How many elements in each list?" << endl;
cin >> N;
cout << "List1" << endl;
for (int i = 1; i <= N; i++)
{
cout << "element :"; cin >> e; L1.push_back(e);
}
cout << "List2" << endl;
for (int i = 1; i <= N; i++)
{
cout << "element :"; cin >> e; L2.push_back(e);
}
combine(L1, L2, L3);
cout << "The result is: ";
for (int i = 0; i < N * 2; i++)
{
cout << L3[i];
} cout << endl;
}// end of main
向量 A 和 B 始终不为空,除非pop_front
简单地说,就像喜欢一样,
if(A.at(0) < B.at(0)) {
C.push_back(A.at(0));
A.pop_front();
}
或者,循环直到 A 和 B 的大小
int ia = 0;
int ib = 0;
while(ia < A.size() && ib < B.size())
{
if(A.at(ia) < B.at(ib))
{
C.push_back(A.at(ia));
ia++;
}
// ...
}
你在这一点上是对的:我认为push_back()
函数会调整向量的大小。
该错误是由于程序中的索引访问无效造成的。
在程序中,循环while
终止条件为while (!A.empty() && !B.empty())
。由于您没有为向量A
或B
删除任何元素,因此终止条件将永远不会满足。这导致无限循环,并进一步导致在向量A
或B
中访问无效索引(取决于,超出ia
或ib
,它们已经跨越了各自向量的实际大小(。
另请注意以下事项:
- 您已经开始访问索引 1 而不是 0 中的元素。向量的索引从 0 开始。
- 条件
if (!A.empty())
,if (!B.empty())
将永远true
.由于您没有从矢量A
和B
中删除元素.
以下是更正的代码。您可以在此处看到它的工作原理:
#include <iostream>
#include <vector>
using namespace std;
// combine two sorted lists A and B into R
// displays comparison every time it is done
void combine(vector<int> A, vector<int> B, vector<int>& R)
{
int ia = 0;
int ib = 0;
int sA = A.size();
int sB = B.size();
while ((ia < sA) && (ib < sB)) {
if (A[ia] < B[ib]) {
R.push_back(A[ia]);
ia++;
}
else {
R.push_back(B[ib]);
ib++;
}
}
while(ia < sA)
{
R.push_back(A[ia++]);
}
while(ib < sB)
{
R.push_back(B[ib++]);
}
cout << "comparison" << endl;
// be careful -- R comes in as an empty vector
}
int main()
{
vector<int> L1;
vector<int> L2;
vector<int> L3;
int N; // how many elements in each of L1 and L2
int e; // for each element
cout << "How many elements in each list?" << endl;
cin >> N;
cout << "List1" << endl;
for (int i = 1; i <= N; i++)
{
cout << "element :"; cin >> e; L1.push_back(e);
}
cout << "List2" << endl;
for (int i = 1; i <= N; i++)
{
cout << "element :"; cin >> e; L2.push_back(e);
}
combine(L1, L2, L3);
cout << "The result is: ";
for (int i = 0; i < N * 2; i++)
{
cout << L3[i] << " | ";
} cout << endl;
}// end of main
以下是经过一些改进并使用iterator
更正的代码。您可以在此处看到它的工作原理:
using namespace std;
#include <iostream>
#include <vector>
// combine two sorted lists A and B into R
// displays comparison every time it is done
void combine(const vector<int>& A, const vector<int>& B, vector<int>& R)
{
auto itA = A.begin();
auto itB = B.begin();
while ( (itA != A.end()) && (itB != B.end()) )
{
if (*itA < *itB)
{
R.push_back(*itA);
itA++;
}
else
{
R.push_back(*itB);
itB++;
}
}
while(itA != A.end())
{
R.push_back(*itA);
itA++;
}
while(itB != B.end())
{
R.push_back(*itB);
itB++;
}
cout << "comparison" << endl;
// be careful -- R comes in as an empty vector
}
int main()
{
vector<int> L1;
vector<int> L2;
vector<int> L3;
int N; // how many elements in each of L1 and L2
int e; // for each element
cout << "How many elements in each list?" << endl;
cin >> N;
cout << "List1" << endl;
for (int i = 0; i < N; i++)
{
cout << "element :"<<endl; cin >> e; L1.push_back(e);
}
cout << endl << "List2" << endl;
for (int i = 0; i < N; i++)
{
cout << "element :"<<endl; cin >> e; L2.push_back(e);
}
combine(L1, L2, L3);
cout << "The result is: ";
for (int i = 0; i < N * 2; i++)
{
cout << L3[i]<<" | ";
}
}// end of main
在您的循环中,您检查向量是否为空,我认为您应该检查它们的计数器是否在范围内或类似的东西。例如,如果向量 A 是 [1,2] 并且 B 是 [3,4,5] 循环两次并插入 A 向量 ia 现在将超出界限,但您仍然进入 if 语句,其中 A[ia] 与 B[ib] 进行比较,我相信这就是您在向量上越界的地方。我也相信矢量索引从 0 开始,这也可能是你的问题。
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将元素添加到数组的线程安全函数?
- 如何将元素添加到向量的字符串位置
- 将数组的元素添加到链表中
- 如何在C++中将元素添加到非固定大小的数组中?
- 如何使用模板根据类型将元素添加到各种容器中
- 将在堆栈上声明的元素添加到静态数组
- 有什么方法可以将元素添加到列表中,如图所示?
- 继承层次结构并将元素添加到向量
- gStreamer 将元素添加到通过 gst_parse_launch 创建的管道中
- 将新元素添加到列表中,并返回对该元素的引用?
- 将元素添加到 std::list 在多线程中,无需 C++ 互斥锁
- C++如何通过超出大小将元素添加到指针数组中
- 将集合的随机元素添加到列表中,然后将其从原始集合中移除
- 将字符串集合中的元素添加到字符串集合的向量中
- 为什么 memcpy() 是一种将元素添加到 'std::map' 的方法?
- 如何有条件地将元素添加到 std::array - C++11
- 如何在 c++ 中将元素添加到数组的最后一个
- 如何使用<<运算符将元素添加到向量?
- 将元素添加到结构向量的全球向量中