将Vector元素复制到动态数组中 [英] copying Vector elements into Dynamic Array

查看:169
本文介绍了将Vector元素复制到动态数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ * C ++入门 - 4 / e


*第4章阵列&指针,练习4.28

*语句

*编写一个程序来读取标准输入并构建一个向量

的整数来自读取的值。分配一个相同的

大小的数组作为向量并将向量中的元素复制到数组中


* /


#include< iostream>

#include< vector>

int main()

{

int iv;

std :: vector< intivec;


while(std :: cin> iv)

{

ivec.push_back(iv);

}


const size_t arr_sz = ivec.size() + 1;

int * pdarr = new int [arr_sz];

int * pbegin = pdarr;

/ *我们将使用" pbegin"以后打印数组元素* /


/ *将向量中的元素分配给动态数组* /

for(std :: vector< int>: :const_iterator iter = ivec.begin();

iter!= ivec.end(); ++ iter)

{

* pdarr ++ = * iter;

}

/ *打印arrar元素* /

std :: cout<< 打印数组元素:" ;;

for(int * q = pbegin; q!= pbegin + arr_sz; ++ q)

{

std :: cout<< * q<< " " ;;

}


std :: cout<< std :: endl;


返回0;

}


========输出===========

~ / programming / cpp $ g ++ -ansi -pedantic -Wall -Wextra ex_04-28.cpp

~ / programming / cpp $ ./a.out

2

3

-9

8

打印数组元素:2 3 -9 8 0

~ / programming / cpp $

i有2个问题:


1.)我使用了vecotor size + 1。对于数组大小,但我没有输入最后的

字符为NULL。该程序是否将它自动添加到数组末尾




2.)输出最后包含一个额外的整数,零,0。我不是因为我使用了q!=

pbegin + arr_size而让指针滑落到数组的末尾。那么零来自哪里?

-

- http://arnuld.blogspot.com

/* C++ Primer - 4/e

* chapter 4- Arrays & Pointers, exercise 4.28
* STATEMENT
* write a programme to read the standard input and build a vector
of integers from values that are read. allocate an array of the same
size as the vector and copy elements from the vector into the array

*/

#include <iostream>
#include <vector>

int main()
{
int iv;
std::vector<intivec;

while(std::cin >iv)
{
ivec.push_back(iv);
}

const size_t arr_sz = ivec.size() + 1;
int* pdarr = new int[arr_sz];
int* pbegin = pdarr;
/* we will use "pbegin" to print the array elements later */

/* assign elements from vector to the dynamic array */
for(std::vector<int>::const_iterator iter=ivec.begin();
iter != ivec.end(); ++iter)
{
*pdarr++ = *iter;
}
/* printing the elements of arrar */
std::cout << "printing array elements: ";
for(int* q = pbegin; q != pbegin + arr_sz; ++q)
{
std::cout << *q << " ";
}

std::cout << std::endl;

return 0;
}

======== OUTPUT ===========
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_04-28.cpp
~/programming/cpp $ ./a.out
2
3
-9
8
printing array elements: 2 3 -9 8 0
~/programming/cpp $
i have 2 questions:

1.) i have used "vecotor size + 1" for array size but i did not enter last
character as NULL. does the programme add it to the end of array itself
automatically ?

2.) output contains an extra integer in the end, the zero, 0. i am not
letting the pointer slipping-off the end of array because i used "q !=
pbegin + arr_size" then from where the zero came ?
--
-- http://arnuld.blogspot.com

推荐答案

g ++ -ansi -pedantic -Wall -Wextra ex_04-28.cpp

~ / programming / cpp
g++ -ansi -pedantic -Wall -Wextra ex_04-28.cpp
~/programming/cpp


./ a.out

2

3

-9

8

打印数组元素:2 3 -9 8 0

~ /编程/ cpp
./a.out
2
3
-9
8
printing array elements: 2 3 -9 8 0
~/programming/cpp




i有两个问题:


1.)我使用了vecotor size + 1对于数组大小,但我没有输入最后的

字符为NULL。该程序是否将它自动添加到数组末尾




2.)输出最后包含一个额外的整数,零,0。我不是因为我使用了q!=

pbegin + arr_size而让指针滑落到数组的末尾。那么零来自哪里?

-

- http://arnuld.blogspot.com


i have 2 questions:

1.) i have used "vecotor size + 1" for array size but i did not enter last
character as NULL. does the programme add it to the end of array itself
automatically ?

2.) output contains an extra integer in the end, the zero, 0. i am not
letting the pointer slipping-off the end of array because i used "q !=
pbegin + arr_size" then from where the zero came ?
--
-- http://arnuld.blogspot.com


这篇关于将Vector元素复制到动态数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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