通过向量C++更改数组中元素的值<int>
Change the value of elements in the array by vector<int> C++
>我使用Visual Studio 2012。我创建了自己的函数,它的工作方式类似于sprintf(&a(。
我需要解决问题:如何交换两个特定的指针元素?
这是我的代码:
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
void swap_spec(vector<int>* a, vector<int>* b)
{
vector<int>* tmp = NULL;
*tmp = *a;
*a = *b;
*b = *tmp;
}
int main()
{
vector<int> test(4);
test[0] = 5;
test[1] = 4;
test[2] = 3;
test[3] = 2;
swap_spec(*test[0], *test[1]);
printf("%d %d", test[0], test[1]);
return 0;
}
我收到一个错误:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
怎么了?我应该改变什么?
我不确定您要做什么,但我猜您正在尝试交换向量中的两个值。正如您的评论所说,使用交换会起作用,但我认为您对代码的实际作用感到困惑。让我们在这里一步一步地走:
vector<int> test(4);
test[0] = 5;
test[1] = 4;
test[2] = 3;
test[3] = 2;
swap_spec(*test[0], *test[1]); // *test[0] is the same as *(test[0])
当您执行 *test[0] 时,这与 *(test[0]( 相同。这意味着取消引用/查看内存地址测试[0]中的值,即5。您无法访问该内存地址,因此会导致 seg 错误。
第二个问题:
void swap_spec(vector<int>* a, vector<int>* b)
{
vector<int>* tmp = NULL;
*tmp = *a;
*a = *b; // This says, get whatever vector is pointed at b, and copy it to the memory location variable a points to.
*b = *tmp;
}
由于您正在传递指向向量的指针,因此您在这里所说的是交换两个向量,而不是它们中的值。但是当你用:
swap_spec(*test[0], *test[1]);
test[0] 的类型是一个 int,*(test[0]( 的类型是一个取消引用的 int(这是一个 seg-fault,但据说是另一个 int 类型(,但参数类型是一个向量 *(指向向量的指针(,这已经与您传入的参数不一致。看看这在多个层面上是如何错误的。
因此,考虑到所有这些信息,看起来您正在尝试交换向量中的两个值。您可以通过以下两种方式之一执行此操作。您可以使用指针执行此操作:
void swap_spec(int *a, int *b) {
int tmp = *a;
*a = *b; // Go to address location where variable a points to, and assign whatever value is at memory location where variable b points to
*b = tmp;
}
swap_spec(&test[0], &test[1]); // Pass in address of where test[0] and test[1] points to
// Note that type of &test[0] is an int * (Consistent with parameter)
或参考资料:
void swap_spec(int &a, int &b) {
int tmp = a;
a = b; // Since you are using references, this will actually modify test[0] at its memory location
b = tmp;
}
swap_spec(test[0], test[1]); // Since you are using references, you don't need to pass in the address.
第二种方式与标准库的交换 (http://www.cplusplus.com/reference/algorithm/swap/( 相同。引用有时(或者可能通常(受到青睐,因为它产生更干净的代码(使用的*运算符更少(,因此混淆更少。
你声明你的函数将指向 vector<int>
s 的指针作为参数,但你随后传递的是别的东西。 *test[1]
正在取消引用test[1]
即 int 4。 您将整数 4 和 5 视为指针,这是有问题的。
目前还不完全清楚您要完成什么,但是如果您只想交换向量的两个元素,为什么不执行以下操作:
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
void swap_spec(vector<int> & a, size_t i, size_t j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
int main()
{
vector<int> test(4);
test[0] = 5;
test[1] = 4;
test[2] = 3;
test[3] = 2;
printf("%d %d n", test[0], test[1]);
swap_spec(test, 0, 1);
printf("%d %d n", test[0], test[1]);
return 0;
}
假设您真正想要做的是交换两个特定的指针元素。然后几乎没有可能的错误。 首先,您创建的向量是一个整数数组,而不是指针向量:
vector<int> test(4);
其次,在函数接口中,输入参数的类型实际上是指向整数向量的指针(这意味着您希望交换两个向量,而不是两个整数指针(。
void swap_spec(vector<int>* a, vector<int>* b)
第三,由于您的向量是整数向量,因此应用间接运算符*
会让您得到错误(*a
意味着给我存储在变量 a
中描述的内存位置中的内容(。
swap_spec(*test[0], *test[1]);
以下是交换"两个整数指针"的方法:
#include <stdio.h>
void swap_spec(int** a, int** b) // the function which swaps int pointers.
{
int** tmp;
*tmp = *a;
*a = *b;
*b = *tmp;
}
int main()
{
int** test = new int*[4]; // create an array of int integers
for (int i = 0; i < 4; ++i) {
test[i] = new int;
}
*test[0] = 5;
*test[1] = 4;
*test[2] = 3;
*test[3] = 2;
printf("%d %dn", *test[0], *test[1]);
swap_spec(&test[0], &test[1]); // pass their addresses instead
printf("%d %dn", *test[0], *test[1]);
for (int i = 0; i < 4; ++i) {
delete test[i];
}
delete[] test;
return 0;
}
如果你想做的只是简单地交换向量中的两个 int 元素,那么std::swap()
或 @Narfanator 和 @Spook 提出的答案应该有效。
好吧,我认为这应该可以让你,但我的 C 有点生疏:
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
void swap_spec(int* a, int* b)
{
int tmp = NULL;
tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
vector<int> test(4);
test[0] = 5;
test[1] = 4;
test[2] = 3;
test[3] = 2;
swap_spec(&test[0], &test[1]);
printf("%d %d", test[0], test[1]);
return 0;
}
我在这里所做的是"交换两个内存位置的值",然后在向量的第一个和第二个元素的位置中传递。
让我们回顾一下您的代码,看看我们是否无法清除某些正在发生的事情:
void swap_spec(vector<int>* a, vector<int>* b)
这说:给我两个pointers
vectors storing ints
。当你处理向量的元素时,你只需传递存储在向量中的类型:在本例中,int
:
void swap_spec(int a, int b)
但是,C/C++ 是按值传递的,这意味着传递的值将被复制,然后在本地使用 - 这意味着,您无法通过传入值来影响整个程序环境。
你得到了这个,这就是你传递指针的原因。
相反,您需要传入pointers to the values
:
void swap_spec(int* a, int* b)
从技术上讲,您是按值传递内存地址。无论如何 - 这允许您在整体程序中与函数外部的内存(以及此变量、对象等(进行交互。
swap_spec(*test[0], *test[1]);
这将传入存储在存储在 test[0]
中的内存位置的值,这是无效的,因为它是 5。
你想要的是:
swap_spec(&test[0], &test[1]);
哪个传入这些值的内存地址,这就是您想要的。
我猜,你想交换向量内的元素,而不是向量本身。所以首先,不要交换std::vector<int> *
,而只是int *
.
其次,这里根本不需要指针。使用函数:
void swap_spec(std::vector<int> & vec, int index1, int index2)
{
int tmp = vec[index1];
vec[index1] = vec[index2];
vec[index2] = tmp;
}
然后这样称呼它:
swap_spec(test, 0, 1);
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中