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<intconta ins 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<int ivec;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ivec ) );
std::cin.clear( );
std::cout << "Enter some integers for LIST: ";
std::list<intil ist;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ilist ) );
std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = true;
std::vector<int >::const_iterat or 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 10 5020
On Oct 9, 12:45 pm, arnuld <geek.arn...@gm ail.comwrote:
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<intconta ins 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<int ivec;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ivec ) );
std::cin.clear( );
std::cout << "Enter some integers for LIST: ";
std::list<intil ist;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ilist ) );
std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = true;
std::vector<int >::const_iterat or 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
-- arnuldhttp://lispmachine.wor dpress.com
You could also try std::equal for containers of the same length.
Also, your code should stop looping as soon as an element doesn't
match.
hth An**********@gm ail.com wrote:
On Oct 9, 12:45 pm, arnuld <geek.arn...@gm ail.comwrote:
>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<intconta ins 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<int ivec; std::copy( std::istream_it erator<int>( std::cin ), std::istream_it erator<int>(), std::back_inser ter( ivec ) );
std::cin.clear( ); std::cout << "Enter some integers for LIST: "; std::list<intil ist; std::copy( std::istream_it erator<int>( std::cin ), std::istream_it erator<int>(), std::back_inser ter( ilist ) );
std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = true; std::vector<int >::const_iterat or 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
-- arnuldhttp://lispmachine.wor dpress.com
You could also try std::equal for containers of the same length.
Also, your code should stop looping as soon as an element doesn't
match.
hth
So, it's easy to say:
if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec .begin(), ivec.end(), ilist.begin());
On Tue, 09 Oct 2007 12:17:16 -0700, red floyd wrote:
So, it's easy to say:
if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec .begin(), ivec.end(), ilist.begin());
I have wrapped everything into 2 functions and I used main(), only to call
them. how about this ?
bool comp_elem( std::vector<int const& ivec, std::list<intco nst& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}
void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}
int main()
{
std::cout << "Enter some integers for VECTOR: ";
std::vector<int ivec;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ivec ) );
std::cin.clear( );
std::cout << "Enter some integers for LIST: ";
std::list<intil ist;
std::copy( std::istream_it erator<int>( std::cin ),
std::istream_it erator<int>(),
std::back_inser ter( ilist ) );
std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = comp_elem( ivec, ilist );
print_result( comp_result );
return 0;
}
-- arnuld http://lispmachine.wordpress.com
In article <pa************ *************** @ippimail.com>, ar****@ippimail .com says...
[ ... just FWIW]
bool comp_elem( std::vector<int const& ivec, std::list<intco nst& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}
return (ivec.size() == ilist.size()) &&
std::equal(ivec .begin(), ivec.end(), ilist.begin());
void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}
static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};
std::cout << strings[result] << std::endl;
--
Later,
Jerry.
The universe is a figment of its own imagination.
On Tue, 09 Oct 2007 23:23:25 -0600, Jerry Coffin wrote:
[ ... just FWIW]
return (ivec.size() == ilist.size()) &&
std::equal(ivec .begin(), ivec.end(), ilist.begin());
...[SNIP]....
static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};
std::cout << strings[result] << std::endl;
hey man, its cool 8-)
-- arnuld http://lispmachine.wordpress.com
;red floyd wrote:
An**********@gm ail.com wrote:
[...]
You could also try std::equal for containers of the same length.
Also, your code should stop looping as soon as an element doesn't
match.
So, it's easy to say:
if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec .begin(), ivec.end(), ilist.begin())
And even easier (and more readable) to say:
return ivec.size() == ilist.size()
&& std::equal( ivec.begin(), ivec.end(), ilist.begin() ) ;
(There's also a function lexographical_c ompare, or something
like that, which might---or might not---be usable here. I'd
look it up quickly, but the file server here is acting up,
and I can't access my online copies of the standard at the
moment.)
--
James Kanze (GABI Software) mailto:ja****** ***@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Oct 10, 2:23 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <pan.2007.10.10 .05.00.52.70... @ippimail.com>,
arn...@ippimail .com says...
[ ... just FWIW]
bool comp_elem( std::vector<int const& ivec, std::list<intco nst& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}
return (ivec.size() == ilist.size()) &&
std::equal(ivec .begin(), ivec.end(), ilist.begin());
void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}
static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};
std::cout << strings[result] << std::endl;
'True' is not always 1, it would be safer to do
std::cout << strings[result ? 1 : 0] << std::endl;
Marjancek
James Kanze wrote:
;red floyd wrote:
>An**********@gm ail.com wrote:
[...]
>>You could also try std::equal for containers of the same length.
>>Also, your code should stop looping as soon as an element doesn't match.
>So, it's easy to say:
>if (ivec.size() != ilist.size()) return false; else return std::equal(ivec .begin(), ivec.end(), ilist.begin())
And even easier (and more readable) to say:
return ivec.size() == ilist.size()
&& std::equal( ivec.begin(), ivec.end(), ilist.begin() ) ;
Picky picky picky. :-)
On Oct 10, 4:23 pm, Marjancek <marjan...@gmai l.comwrote:
On Oct 10, 2:23 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <pan.2007.10.10 .05.00.52.70... @ippimail.com>,
arn...@ippimail .com says...
[...]
static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};
std::cout << strings[result] << std::endl;
'True' is not always 1,
In C++ it is. The standard requires it.
it would be safer to do
std::cout << strings[result ? 1 : 0] << std::endl;
If the type being tested is not a bool, then something like:
std::cout << strings[ result != 0 ] << std::endl ;
might be appropriate. But for a bool, true is guaranteed to
convert to 1, and false to 0.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andreas Krueger |
last post by:
Hi!
I am fed up with
vector<int> iv;
iv.push_back(42); iv.push_back(9); iv.push_back(11); ...
and would rather use a function "fillVector":
vector<int> iv = fillVector(42,9,11);
like in Mathematica(tm): someList={42,9,11};
|
by: Erik Borgstr?m |
last post by:
Hi,
Yes, I have followed some of the discussion on vector<vector<int> >,
but perhaps I'm just blind. I want to use a vector<vector<int> > of
outer size n and inner size m. Are these correct ways to achieve this?
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
|
by: Alex Vinokur |
last post by:
------ foo.cpp ------
#include <vector>
using namespace std;
int main()
{
const vector<int> v1 (10);
const vector<bool> v2 (10);
&v1;
|
by: Anonymous |
last post by:
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
|
by: pmatos |
last post by:
Hi all,
I have a vector of vector of ints, I could use C approach by using
int but I think C++ vector<vector<int> > would be easier to manage.
So I have a function which creates and initializes the vector with the
values I need (I know these values before hand).
- What's the best way to initialize the vector<vector<int> >? Can I
initilize it by enumerating its values?
- If I do: v = new vector<vector<int> >(3) for example, is it
| |
by: danielhdez14142 |
last post by:
Some time ago, I had a segment of code like
vector<vector<int example;
f(example);
and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing
vector<vector<int example;
example.push_back(vector<int>());
|
by: citystud |
last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
|
by: arnuld |
last post by:
WANTED:
/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.
|
by: hanna88 |
last post by:
what's the best way to convert a list of string to a vector<int>?
what i have now is for loop and iterate each char in the string
string base="";
//i have vector<int>row,vector < vector<int>row > mat;
//what i want to do with the string is that everytime char != ";"
row.pushback(str that convert to int)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |