关于指针复制的帮助 [英] help about pointer copy

查看:56
本文介绍了关于指针复制的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个数组,我想调整大小(删除一些元素)。我使用

以下代码。你能帮我检查一下是否正确。


////////////// start

int * p1 = new int [oldsize];


// ....

//设置p1的值


int * p2 = p1;

p1 = new int [newsize];


//将一些值从p2复制到p1

for(i = 0; i ....)

if(....)p1 [i] = p2 [i];


删除[] p2;


//////////////结束


假设我有一个数组,例如int * p1 = new int [10];是否有任何简单的方法

来重置p1的长度? (例如,p1变为int [20]并且前10个元素保持原始值。)


非常感谢您的帮助!


JJ

Hi,

I have an array and I want to adjust the size (remove some elements). I use
the following code. Can you help me to check whether it''s correct.

////////////// start
int *p1 = new int[oldsize];

// ....
// set the values of p1

int *p2 = p1;
p1 = new int[newsize];

// copy some values from p2 to p1
for ( i = 0; i .... )
if (....) p1[i] = p2[i];

delete []p2;

////////////// end

Suppose I have an array, e.g., int *p1 = new int[10]; is there any easy way
to reset the length of p1? (e.g, p1 becomes int[20] and the first 10
elements keep the original value).

Thank you very much in advance for your help!

JJ

推荐答案

" Jinjun Xu" < JJ ** @ ucla.edu>写道...
"Jinjun Xu" <jj**@ucla.edu> wrote...
我有一个数组,我想调整大小(删除一些元素)。我
使用以下代码。你能帮我检查一下是否正确。

//////////////开始
int * p1 = new int [oldsize];

// ....
//设置p1的值
int * p2 = p1;
p1 = new int [newsize] //
//将一些值从p2复制到p1
for(i = 0; i ....)
if(....)p1 [i] = p2 [i];

删除[] p2;

//////////////结束


看起来很好。

假设我有一个数组,例如int * p1 = new int [10];是否有任何简单的
方法来重置p1的长度? (例如,p1变为int [20],前10个元素保持原始值。)
I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it''s correct.

////////////// start
int *p1 = new int[oldsize];

// ....
// set the values of p1

int *p2 = p1;
p1 = new int[newsize];

// copy some values from p2 to p1
for ( i = 0; i .... )
if (....) p1[i] = p2[i];

delete []p2;

////////////// end
Seems fine.
Suppose I have an array, e.g., int *p1 = new int[10]; is there any easy way to reset the length of p1? (e.g, p1 becomes int[20] and the first 10
elements keep the original value).




你不需要改变任何东西那种情况。只是认为你有更少的元素,只使用你想象的那么多。并且,不,

没有简单的方法来重置长度一个动态数组。


Victor



You don''t have to change anything in that case. Just think you have
fewer elements and only use as many as you think there are. And, no,
there is no "easy way to reset the length" of a dynamic array.

Victor


inline ...


" ;许俊君 < JJ ** @ ucla.edu>在消息中写道

news:c9 ********** @ daisy.noc.ucla.edu ...
inline...

"Jinjun Xu" <jj**@ucla.edu> wrote in message
news:c9**********@daisy.noc.ucla.edu...


我有一个数组,我想调整大小(删除一些元素)。我
使用以下代码。你能帮我检查一下是否正确。

//////////////开始
int * p1 = new int [oldsize];

// ....
//设置p1的值
int * p2 = p1;
p1 = new int [newsize] //
//将一些值从p2复制到p1
for(i = 0; i ....)
if(....)p1 [i] = p2 [i];

删除[] p2;

//////////////结束

假设我有一个数组,例如,int * p1 = new int [10];是否有任何简单的
方法来重置p1的长度? (例如,p1变为int [20],前10个元素保持原始值。)

非常感谢您的帮助!

JJ
Hi,

I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it''s correct.

////////////// start
int *p1 = new int[oldsize];

// ....
// set the values of p1

int *p2 = p1;
p1 = new int[newsize];

// copy some values from p2 to p1
for ( i = 0; i .... )
if (....) p1[i] = p2[i];

delete []p2;

////////////// end

Suppose I have an array, e.g., int *p1 = new int[10]; is there any easy way to reset the length of p1? (e.g, p1 becomes int[20] and the first 10
elements keep the original value).

Thank you very much in advance for your help!

JJ




简单的方法是使用向量而不是数组。数组有一个固定的

大小限制,而矢量容器动态调整自身大小。

此外,所有STL容器都配备了强大的成员

函数,迭代器和算法交换,排序,替换,追加,推送,

pop等...


作为使用整数向量的简单示例显示功能:


#include< iostream>

#include< vector>


无效显示(std :: vector< int>& r_v)

{

std :: cout<< 矢量内容: << std :: endl;


std :: vector< int> :: iterator it;

for(it = r_v.begin(); it!= r_v.end();它++)

{

std :: cout<< *它<< std :: endl;

}

}


int main()

{

std :: vector< int> vi;


//添加一些元素

vi.push_back(0);

vi.push_back(1);

vi.push_back(2);


std :: cout<< vector vi has << vi.size()<< "元素" << std :: endl;

display(vi);


//添加更多元素

vi.push_back(3);

vi.push_back(4);

vi.push_back(5);


std :: cout<< std :: endl<< vector vi has << vi.size()<< "元素

<< std :: endl;

display(vi);


std :: cout<< std :: endl;


返回0;

}



The easy way is to use a vector instead of an array. Arrays have a fixed
size limitation while a vector container dynamically resizes itself.
Additionally, all STL containers come equipped with powerful member
functions, iterators and algorithms to swap, sort, replace, append, push,
pop, etc...

as a simple example using a vector of integers and a display function:

#include <iostream>
#include <vector>

void display(std::vector<int>& r_v)
{
std::cout << "vector contents:" << std::endl;

std::vector<int>::iterator it;
for (it = r_v.begin(); it != r_v.end(); it++)
{
std::cout << *it << std::endl;
}
}

int main()
{
std::vector<int> vi;

// add a few elements
vi.push_back(0);
vi.push_back(1);
vi.push_back(2);

std::cout << "vector vi has " << vi.size() << " elements" << std::endl;
display(vi);

// add more elements
vi.push_back(3);
vi.push_back(4);
vi.push_back(5);

std::cout << std::endl << "vector vi has " << vi.size() << " elements"
<< std::endl;
display(vi);

std::cout << std::endl;

return 0;
}


"徐进军< JJ ** @ ucla.edu>在留言新闻中写道:< c9 ********** @ daisy.noc.ucla.edu> ...
"Jinjun Xu" <jj**@ucla.edu> wrote in message news:<c9**********@daisy.noc.ucla.edu>...


我有一个数组,我想调整大小(删除一些元素)。我使用以下代码。你能帮我检查一下是否正确。

//////////////开始
int * p1 = new int [oldsize];

// ....
//设置p1的值
int * p2 = p1;
p1 = new int [newsize] //
//将一些值从p2复制到p1
for(i = 0; i ....)
if(....)p1 [i] = p2 [i];

删除[] p2;

//////////////结束

假设我有一个数组,例如,int * p1 = new int [10];是否有任何简单的方法来重置p1的长度? (例如,p1变为int [20],前10个元素保持原始值。)
Hi,

I have an array and I want to adjust the size (remove some elements). I use
the following code. Can you help me to check whether it''s correct.

////////////// start
int *p1 = new int[oldsize];

// ....
// set the values of p1

int *p2 = p1;
p1 = new int[newsize];

// copy some values from p2 to p1
for ( i = 0; i .... )
if (....) p1[i] = p2[i];

delete []p2;

////////////// end

Suppose I have an array, e.g., int *p1 = new int[10]; is there any easy way
to reset the length of p1? (e.g, p1 becomes int[20] and the first 10
elements keep the original value).



不,没有一种简单的方法可以用原始指针做到这一点。

但是你可以使用STL容器std :: vector

或std :: deque做你想做的事情,当你向它们添加数据时它会自动增长。

它们还具有确保您有足够空间来容纳

最小元素数的功能。但是,有一点需要注意的是,每次他们增长时,任何指针(或其他迭代器)都会变成容器状态。

无效。


HTH,Dave Moore


No, there is not a straightforward way to do this with raw pointers.
However you can do what you want using the STL containers std::vector
or std::deque, which will grow automatically as you add data to them.
They also have functions to ensure you have enough space to hold a
minimum number of elements. However, one caveat is that each time
they grow, any pointers (or other iterators) into the container become
invalid.

HTH, Dave Moore


这篇关于关于指针复制的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆