问题描述
这是一个非常丑陋的黑客,但现在我能够想出来了
:(并且它确实需要工作。我想改进程序,我是/>
知道你有更好的想法;-)
/ * C ++ Primer - 4 / e
*
*练习9.20
*声明:
*编写一个程序来比较一个向量< int包含
*相同的元素作为列表< int> ;。
*
* /
#include< iostream>
#include< vector>
#include< list>
#include< algorithm>
#include< iterator>
int main()
{
std :: cout<< 为VECTOR输入一些整数:;
std :: vector< intivec;
std :: copy(std :: istream_iterator< int>(std ::) cin),
std :: istream_iterator< int>(),
std :: back_inserter(ivec));
std :: cin.clear();
std :: cout<< 为LIST输入一些整数:;
std :: list< intilist;
std :: copy(std :: istream_iterator< int>(std ::) cin),
std :: istream_iterator< int>(),
std :: back_inserter(ilist));
std :: cout<< \ n现在我们将比较VECTOR& LIST for equality ::" ;;
bool comp_result = true;
std :: vector< int> :: const_iterator viter = ivec.begin() ;
std :: list< int> :: const_iterator liter = ilist.begin();
/ *如果尺寸不相等,那么它们不能相等: - )* /
if(ivec.size()!= ilist.size())
{
comp_result = false;
}
其他
{
for(;(viter!= ivec.end())||(升! = ilist.end());
++ viter,++升)
{
if(* viter!= *升)
{
comp_result = false;
}
}
}
if(comp_result)
{
std :: cout<< VECTOR& LIST是EQUAL << std :: endl;
}
else
{
std :: cout<< VECTOR&列表不相等 << std :: endl;
}
返回0;
}
==== =====几次运行=================
~ / programming / c ++ $ g ++ -ansi -pedantic -Wall -Wextra ex_09- 20.cpp
~ / programming / c ++ $ ./a.out
为VECTOR输入一些整数:1 2 3
输入一些整数列表:1 2 1
现在我们将比较VECTOR&平等列表:: VECTOR&列表不相等
~ / programming / c ++ $ ./a.out
为VECTOR输入一些整数:1
输入一些整数列表:1 2 3 4 5 6 7 8 9 0 9 9 8 8 8 8 8 8 8 8 8 8
8 8 8 88 88 88 8 8 8 88 8 8 88 8 8 8 8 8 8 8 8
现在我们将比较VECTOR&平等列表:: VECTOR&列表不相等
~ / programming / c ++ $ ./a.out
为VECTOR输入一些整数:1 2 3
输入一些LIST的整数:1 2 3
现在我们将比较VECTOR&平等列表:: VECTOR& LIST是EQUAL
~ / programming / c ++ $ ./a.out
为VECTOR输入一些整数:1 2 3
输入一些整数列表:1 2
现在我们将比较VECTOR&平等列表:: VECTOR&列表不等于
等于
- arnuld
It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)
/* C++ Primer - 4/e
*
* exercise 9.20
* STATEMENT:
* Write a program to to compare whether a vector<intcontains the
* same elements as a list<int>.
*
*/
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
std::cout << "Enter some integers for VECTOR: ";
std::vector<intivec;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ivec ) );
std::cin.clear();
std::cout << "Enter some integers for LIST: ";
std::list<intilist;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ilist ) );
std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = true;
std::vector<int>::const_iterator viter = ivec.begin();
std::list<int>::const_iterator liter = ilist.begin();
/* If sizes are unequal, then they can not be equal :-) */
if( ivec.size() != ilist.size() )
{
comp_result = false;
}
else
{
for( ; ( viter != ivec.end() ) || ( liter != ilist.end() );
++viter, ++liter )
{
if( *viter != *liter )
{
comp_result = false;
}
}
}
if( comp_result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
return 0;
}
========= A FEW RUNS =================
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_09-20.cpp
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 1
Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1
Enter some integers for LIST: 1 2 3 4 5 6 7 8 9 0 9 9 8 8 8 8 8 8 8 8 8 8
8 8 8 88 88 88 8 8 8 88 8 8 88 8 8 8 8 8 8 8 8
Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 3
Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are EQUAL
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2
Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT
equal
-- arnuld
http://lispmachine.wordpress.com
推荐答案
这篇关于比较向量< int>的元素并列出< int>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!