C ++ Primer第4版,ex4.3.1-4.28 [英] C++ Primer 4th edition, ex4.3.1-4.28

查看:55
本文介绍了C ++ Primer第4版,ex4.3.1-4.28的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

练习4.28:编写一个程序来读取标准输入,并从读取的值构建一个内含向量的元素。分配一个与向量相同的

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

数组中。


这里是我写的代码。有没有人有更好的主意?


#include< iostream>

#include< vector>


int main(){

const unsigned ivec_sz = 6;

std :: vector< intivec(ivec_sz);

unsigned j;


//向量添加元素

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

iter!= ivec.end();

++ iter)

{

std :: cout<< 输入一个整数:;

std :: cin> j;

* iter = j;

}


//创建一个与ivec相同大小的数组

const unsigned arr_sz = ivec_sz;

int ia [arr_sz];


//从向量中读取&安培;写入数组

int * pia = ia;

std :: vector< int> :: iterator iter_ivec = ivec.begin();


for(int * pbegin = ia,* pend = ia + arr_sz;

* pbegin!= * pend;

++ pbegin )

{

* pbegin = * iter_ivec;

++ iter_ivec;

}


std :: cout<< 打印阵列元素: << std :: endl;

for(int * pbegin = ia,* pend = ia + arr_sz;

* pbegin!= * pend;

++ pbegin)

{

std :: cout<< 数组元素: << * pbegin<< std :: endl;

}


}


--arnuld
http://arnuld.blogspot.com

解决方案

arnuld写道:


练习4.28:编写一个程序来读取标准输入并构建一个

来自读取值的int的向量。分配一个与向量相同的

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

数组中。


这里是我写的代码。有没有人有更好的主意?


#include< iostream>

#include< vector>


int main(){

const unsigned ivec_sz = 6;

std :: vector< intivec(ivec_sz);

unsigned j;


//向量添加元素

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

iter!= ivec.end();

++ iter)

{

std :: cout<< 输入一个整数:;

std :: cin> j;

* iter = j;

}


//创建一个与ivec相同大小的数组

const unsigned arr_sz = ivec_sz;

int ia [arr_sz];


//从向量中读取&安培;写入数组

int * pia = ia;

std :: vector< int> :: iterator iter_ivec = ivec.begin();


for(int * pbegin = ia,* pend = ia + arr_sz;

* pbegin!= * pend;

++ pbegin )

{

* pbegin = * iter_ivec;

++ iter_ivec;

}


std :: cout<< 打印阵列元素: << std :: endl;

for(int * pbegin = ia,* pend = ia + arr_sz;

* pbegin!= * pend;

++ pbegin)

{

std :: cout<< 数组元素: << * pbegin<< std :: endl;

}


}



#include< algorithm>

#include< iterator>

#include< iostream>

#include< vector>


int main(void){

//从标准输入中读取整数的向量:

std :: vector< inti_vect(std :: istream_iterator< ; int>(std :: cin),

(std :: istream_iterator< int>()));

//创建一个相同大小的动态数组:

int * i_array = new int [i_vect.size()];

//并将向量中的元素复制到数组中:

std :: copy(i_vect.begin(),i_vect.end(),i_array);


//健全性检查:输出数组

std :: copy(i_array,i_array + i_vect.size(),

std :: ostream_iterator< int>(std :: cout,""));

std :: cout<< ''\\ n';;

}


news_groupecho 1 2 3 4 | a.out

1 2 3 4


Best


Kai-Uwe Bux


Kai-Uwe Bux写道:


arnuld写道:


>练习4.28:编写一个程序来读取标准输入,并根据读取的值构建一个int向量。分配一个与向量大小相同的数组,并将向量中的元素复制到数组中。

这里是我编写的代码。有没有人有更好的主意?

#include< iostream>
#include< vector>

int main(){
const unsigned ivec_sz = 6;
std :: vector< intivec(ivec_sz);
unsigned j;

//为向量添加元素
for(std :: vector< int> :: iterator iter = ivec.begin();
iter!= ivec.end();
++ iter)
{
std :: cout<< ; 输入一个整数:;
std :: cin> j;
* iter = j;
}
//创建一个相同的数组大小为ivec
const unsigned arr_sz = ivec_sz;
int ia [arr_sz];

//从向量读取&安培;写入数组
int * pia = ia;
std :: vector< int> :: iterator iter_ivec = ivec.begin();

for(int * pbegin = ia,* pend = ia + arr_sz;
* pbegin!= * pend;
++ pbegin)
{
* pbegin = * iter_ivec;
++ iter_ivec;
}

std :: cout<< 打印阵列元素: << std :: endl;
for(int * pbegin = ia,* pend = ia + arr_sz;
* pbegin!= * pend;
++ pbegin)
{
std :: cout<< 数组元素: << * pbegin<< std :: endl;
}
}



#include< algorithm>

# include< iterator>

#include< iostream>

#include< vector>


int main(void) {

//构建一个从标准输入读取的int的向量:

std :: vector< inti_vect(std :: istream_iterator< int>(std :: cin ),

(std :: istream_iterator< int>()));

//创建一个相同大小的动态数组:

int * i_array = new int [i_vect.size()];

//并将向量中的元素复制到数组中:

std :: copy(i_vect。 begin(),i_vect.end(),i_array);


//健全性检查:输出数组

std :: copy(i_array,i_array + i_vect.size(),

std :: ostream_iterator< int>(std :: cout,""));

std :: cout<< \\\
;



我累了:


删除[] i_array;


[并不是说它有任何区别,只是为了保持良好的习惯。然后再次,

如何使用int *而不是vector< inta好习惯?]


}

news_groupecho 1 2 3 4 | a.out

1 2 3 4



Best


Kai-Uwe Bux


Kai-Uwe Bux写道:


#include< algorithm>

#include< iterator>

#include< iostream>

#include< vector>


int main(void){

//从标准输入中读取整数的向量:

std :: vector< inti_vect(std :: istream_iterator< ; int>(std :: cin),

(std :: istream_iterator< int>()));

//创建一个相同大小的动态数组:

int * i_array = new int [i_vect.size()];

//并将向量中的元素复制到数组中:

std :: copy(i_vect.begin(),i_vect.end(),i_array);


//健全性检查:输出数组

std :: copy(i_array,i_array + i_ve ct.size(),

std :: ostream_iterator< int>(std :: cout," " ));

std :: cout<< \\\
;这是该程序的输出:
unix @ debian:〜/ programming / cpp / BLOCKQUOTE>

Exercise 4.28: Write a program to read the standard input and build a
vector of ints from values that are read. Allocate an array of the same
size as the vector and copy the elements from the vector into the
array.

here is the code i wrote. does anyone has a better idea?

#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<intivec(ivec_sz);
unsigned j;

// adding elements to vector
for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << "Enter an integer: ";
std::cin >j;
*iter = j;
}

// creating an array of same size as "ivec"
const unsigned arr_sz = ivec_sz;
int ia[arr_sz];

// reading from "vector" & writing into the "array"
int *pia = ia;
std::vector<int>::iterator iter_ivec=ivec.begin();

for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
*pbegin = *iter_ivec;
++iter_ivec;
}

std::cout << "Printing array elements: " << std::endl;
for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
std::cout << "array element: " << *pbegin << std::endl;
}

}

--arnuld
http://arnuld.blogspot.com

解决方案

arnuld wrote:

Exercise 4.28: Write a program to read the standard input and build a
vector of ints from values that are read. Allocate an array of the same
size as the vector and copy the elements from the vector into the
array.

here is the code i wrote. does anyone has a better idea?

#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<intivec(ivec_sz);
unsigned j;

// adding elements to vector
for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << "Enter an integer: ";
std::cin >j;
*iter = j;
}

// creating an array of same size as "ivec"
const unsigned arr_sz = ivec_sz;
int ia[arr_sz];

// reading from "vector" & writing into the "array"
int *pia = ia;
std::vector<int>::iterator iter_ivec=ivec.begin();

for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
*pbegin = *iter_ivec;
++iter_ivec;
}

std::cout << "Printing array elements: " << std::endl;
for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
std::cout << "array element: " << *pbegin << std::endl;
}

}

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_vect ( std::istream_iterator<int>( std::cin ),
(std::istream_iterator<int>()) );
// create a dynamic array of the same size:
int* i_array = new int [ i_vect.size() ];
// and copy the elements from the vector into the array:
std::copy( i_vect.begin(), i_vect.end(), i_array );

// sanity check: output the array
std::copy( i_array, i_array + i_vect.size(),
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << ''\n'';
}

news_groupecho 1 2 3 4 | a.out
1 2 3 4

Best

Kai-Uwe Bux


Kai-Uwe Bux wrote:

arnuld wrote:

>Exercise 4.28: Write a program to read the standard input and
build a vector of ints from values that are read. Allocate an array of
the same size as the vector and copy the elements from the vector into
the array.

here is the code i wrote. does anyone has a better idea?

#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<intivec(ivec_sz);
unsigned j;

// adding elements to vector
for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << "Enter an integer: ";
std::cin >j;
*iter = j;
}

// creating an array of same size as "ivec"
const unsigned arr_sz = ivec_sz;
int ia[arr_sz];

// reading from "vector" & writing into the "array"
int *pia = ia;
std::vector<int>::iterator iter_ivec=ivec.begin();

for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
*pbegin = *iter_ivec;
++iter_ivec;
}

std::cout << "Printing array elements: " << std::endl;
for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
std::cout << "array element: " << *pbegin << std::endl;
}

}


#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_vect ( std::istream_iterator<int>( std::cin ),
(std::istream_iterator<int>()) );
// create a dynamic array of the same size:
int* i_array = new int [ i_vect.size() ];
// and copy the elements from the vector into the array:
std::copy( i_vect.begin(), i_vect.end(), i_array );

// sanity check: output the array
std::copy( i_array, i_array + i_vect.size(),
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << ''\n'';

I am getting tired:

delete [] i_array;

[Not that it makes any difference, but just to keep good habits. Then again,
how is using int* instead of vector<inta good habit anyway?]

}

news_groupecho 1 2 3 4 | a.out
1 2 3 4

Best

Kai-Uwe Bux


Kai-Uwe Bux wrote:

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_vect ( std::istream_iterator<int>( std::cin ),
(std::istream_iterator<int>()) );
// create a dynamic array of the same size:
int* i_array = new int [ i_vect.size() ];
// and copy the elements from the vector into the array:
std::copy( i_vect.begin(), i_vect.end(), i_array );

// sanity check: output the array
std::copy( i_array, i_array + i_vect.size(),
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << ''\n'';

this is the output of this programme:

unix@debian:~/programming/cpp


这篇关于C ++ Primer第4版,ex4.3.1-4.28的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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