473,769 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cout << vector<string>


In the boost::program_ options tutorial, the author included the following
code:
cout << "Input files are: "
<< vm["input-file"].as< vector<string()
<< "\n";
Basically, he is trying to print a vector of string, in one line. I could
not get this compile (let alone run). To get it to compile and run, I had
to re-write it as:
vector<stringv = vm["input-file"].as< vector<string() ;

vector<string>: :const_iterator i;

for ( i = v.begin();
i != v.end();
++i )
{
cout << *citer << " ";
}
Does anyone know if the original line of code is correct? If so, what was I
missing?

Nov 6 '08
42 4547
James Kanze wrote:
On Nov 7, 1:32 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
>Pete Becker wrote:
>>On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegoru sh...@gmail.com said:
The example probably assumes there is an overloaded operator<<() for
std::ostre am and std::vector<>, something like this:
namespace std {
template<class A1, class A2>
ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
Which has undefined behavior. You can only add template specializations
to namespace std when they depend on user-defined types.
>The correct alternative, AIUI:
>#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
>template<typen ame T>
std::ostream & operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
}
return out;
}
>int main() {
int const ints[] = { 1, 2, 3, 4 };
std::cout << std::vector<int >( ints, ints + 4 ) << '\n';
}

Correct in what sense? It's OK for simple test, like this, but
it's certainly not something you'd allow in production code.
Correct in the sense that it implements the OP's intent, and is allowed
by the standard. As far as using it in production code... Ad hoc
output of this sort only really comes up for debugging purposes. If you
don't even think it's OK for debug, I'd love to know your suggested
alternative.
Nov 7 '08 #11
rio

"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49******** *************** @read.cnntp.org ...
Juha Nieminen wrote:
>Jeff Schwab wrote:
>>template<type name T>
std::ostrea m& operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
}
return out;
}

Is there some advantage of that code over a shorter and simpler:

template<typen ame T>
std::ostream & operator<<(std: :ostream& out, std::vector<Tco nst& v) {
for(std::size_t i = 0; i < v.size()-1; ++i)

Nit: std::size_t is not guaranteed to be vector::size_ty pe.
so could be
std::size_t != vector::size_ty pe
and what could happen if in the definition
size_t sii=big;
int a[sii];
vector<int b(a, a+sii);
> out << v[i] << " ";
out << v.back();

I think, v.back() has undefined behavior if the v is empty.
> return out;
}


Best

Kai-Uwe Bux


Nov 9 '08 #12
rio

"Jeff Schwab" <je**@schwabcen ter.comha scritto nel messaggio
news:Q7******** *************** *******@giganew s.com...
Pete Becker wrote:
>On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<ma*********** ****@gmail.coms aid:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

template<typena me T>
std::ostream& operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
what is the meaning of "out << v.back();"
why not write above only?
copy(v.begin(), v.end(), out_iter( out, " " ));

why it is not easy?

is it not better the "operator<< " defined below?

#include <stdio.h>
#include <iostream.h>
#include <iterator.h>
#include <vector.h>

template<typena me T>
std::ostream& operator<<(std: :ostream& out, std::vector<T>c onst& v)
{size_t i;
for(i=0; i<v.size(); ++i)
out<<v[i]<<" ";
return out;
}

int main()
{int ints[]={1,2,3,4}, inty2[100]={0}, i;
std::vector<int vettore(inty2, inty2+100);
std::vector<int vettor1(100);

for(i=0; i<90; ++i)
vettor1[i]=i;

std::cout << std::vector<int >(ints,ints+4 ) << '\n';
std::cout << vettore << '\n';
std::cout << "Vettor1" << vettor1 << '\n';

getchar();
}

the above seems ok what not seems ok to me that this
"
std::vector<int vettor1;

for(i=0; i<90; ++i)
vettor1[i]=i;
"
here goes in segmentation fault
is not vector1 self sizabile?

than how you are sure that *all* the allocations have its deallocation?
for example it seems "std::vector<in t>(ints,ints+4) "
copy in new memory the array ints[0..3]
and than it has to deallocate it but when and where
}
return out;
}

int main() {
int const ints[] = { 1, 2, 3, 4 };
std::cout << std::vector<int >( ints, ints + 4 ) << '\n';
}


Nov 9 '08 #13
rio

"rio" <a@b.cha scritto nel messaggio
news:49******** *************** @reader5.news.t in.it...
>
"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49******** *************** @read.cnntp.org ...
>Juha Nieminen wrote:
>>Jeff Schwab wrote:
template<typ ename T>
std::ostream & operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
}
return out;
}

Is there some advantage of that code over a shorter and simpler:

template<type name T>
std::ostrea m& operator<<(std: :ostream& out, std::vector<Tco nst& v) {
for(std::size_t i = 0; i < v.size()-1; ++i)

Nit: std::size_t is not guaranteed to be vector::size_ty pe.
i know max(std::size_t ) >= max(vector::siz e_type)
i know v.size()-1<=max(vector:: size_type) <= max(std::size_t )

so size_t is ok

Nov 10 '08 #14
rio wrote:
>
"rio" <a@b.cha scritto nel messaggio
news:49******** *************** @reader5.news.t in.it...
>>
"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49******* *************** *@read.cnntp.or g...
>>Juha Nieminen wrote:

Jeff Schwab wrote:
template<ty pename T>
std::ostrea m& operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
}
return out;
}

Is there some advantage of that code over a shorter and simpler:

template<typ ename T>
std::ostream & operator<<(std: :ostream& out, std::vector<Tco nst& v) {
for(std::size_t i = 0; i < v.size()-1; ++i)

Nit: std::size_t is not guaranteed to be vector::size_ty pe.

i know max(std::size_t ) >= max(vector::siz e_type)
Could you provide a pointer into the standard to backup that claim? or are
you making a statement about a particular platform?

i know v.size()-1<=max(vector:: size_type) <= max(std::size_t )

so size_t is ok

Best

Kai-Uwe Bux
Nov 10 '08 #15
On Nov 7, 12:17*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegorush ...@gmail.comsa id:


The example probably assumes there is an overloaded operator<<() for
std::ostream and std::vector<>, something like this:
* * #include <ostream>
* * #include <iterator>
* * #include <algorithm>
* * namespace std {
* * * * template<class A1, class A2>
* * * * ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
* * * * {
* * * * * * copy(vec.begin( ), vec.end(), ostream_iterato r<A1>(s, "
"));
* * * * * * return s;
* * * * }
* * }

Which has undefined behavior. You can only add template specializations
to namespace std when they depend on user-defined types.
Formally true.

On practice it works just fine as long as One Definition Rule is not
violated.

--
Max
Nov 10 '08 #16
On Nov 7, 7:21 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
James Kanze wrote:
On Nov 7, 1:32 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
Pete Becker wrote:
On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegorus h...@gmail.coms aid:
The example probably assumes there is an overloaded operator<<() for
std::ostrea m and std::vector<>, something like this:
namespace std {
template<class A1, class A2>
ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
Which has undefined behavior. You can only add template
specialization s to namespace std when they depend on
user-defined types.
The correct alternative, AIUI:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
template<typena me T>
std::ostream& operator<<(std: :ostream& out, std::vector<Tco nst& v) {
if (!v.empty()) {
typedef std::ostream_it erator<Tout_ite r;
copy(v.begin(), v.end() - 1, out_iter( out, " " ));
out << v.back();
}
return out;
}
int main() {
int const ints[] = { 1, 2, 3, 4 };
std::cout << std::vector<int >( ints, ints + 4 ) << '\n';
}
Correct in what sense? It's OK for simple test, like this,
but it's certainly not something you'd allow in production
code.
Correct in the sense that it implements the OP's intent, and
is allowed by the standard. As far as using it in production
code... Ad hoc output of this sort only really comes up for
debugging purposes. If you don't even think it's OK for
debug, I'd love to know your suggested alternative.
Do you leave such debugging code in your production code? If
not, no problem, but there's no point in making the << a
template. If so:

-- It doesn't work. Try it with std::vector<
std::vector< int , for example.

-- It very rapidly introduces undefined behavior, as you write
the above, and some other programmer provides the same
function, but with a comma as a separator.

For a quick debugging session, the simplest solution is to knock
out a non-template version in an unnamed namespace. For
anything else, you really need to define what is wanted; you
don't output raw vectors, but vectors with a semantic
signification, which determines how you format them. If you're
using vectors to represent both sets and sequences in a
mathematical context, for example, you'll doubtlessly have a
class for each, with the vector buried deep down in it. And
those classes will provide the formatted output.

If you find that you often need such debugging output (I don't,
but I'm sure that there are applications that to), then you
might want to come up with a general FormattedSequen ce class
template, with a template function to generate it (so you get
automatic type deduction); this could, of course, be in any
namespace you want.

--
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
Nov 10 '08 #17
On Nov 10, 10:19 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
On Nov 7, 12:17 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegorush ...@gmail.comsa id:
The example probably assumes there is an overloaded
operator<<() for std::ostream and std::vector<>, something
like this:
#include <ostream>
#include <iterator>
#include <algorithm>
namespace std {
template<class A1, class A2>
ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
{
copy(vec.begin( ), vec.end(), ostream_iterato r<A1>(s, "
"));
return s;
}
}
Which has undefined behavior. You can only add template
specializations to namespace std when they depend on
user-defined types.
Formally true.
On practice it works just fine as long as One Definition Rule
is not violated.
Taking into account all of the code, including that which you
can't see or know about, such as in the implementation of the
library.

In other words, it works just fine as long as you're lucky.

--
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
Nov 10 '08 #18
rio

"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49******** *************** @read.cnntp.org ...
rio wrote:
>i know max(std::size_t ) >= max(vector::siz e_type)

Could you provide a pointer into the standard to backup that claim? or are
you making a statement about a particular platform?
seems to me
should be
in the C standard in the definition of type size_t
>i know v.size()-1<=max(vector:: size_type) <= max(std::size_t )

so size_t is ok


Best

Kai-Uwe Bux

Nov 10 '08 #19
On Nov 10, 10:22*am, James Kanze <james.ka...@gm ail.comwrote:
On Nov 10, 10:19 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
On Nov 7, 12:17 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-11-07 06:03:15 -0500, Maxim Yegorushkin
<maxim.yegorush ...@gmail.comsa id:
The example probably assumes there is an overloaded
operator<<() for std::ostream and std::vector<>, something
like this:
* * #include <ostream>
* * #include <iterator>
* * #include <algorithm>
* * namespace std {
* * * * template<class A1, class A2>
* * * * ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
* * * * {
* * * * * * copy(vec.begin( ), vec.end(), ostream_iterato r<A1>(s, "
"));
* * * * * * return s;
* * * * }
* * }
Which has undefined behavior. You can only add template
specializations to namespace std when they depend on
user-defined types.
Formally true.
On practice it works just fine as long as One Definition Rule
is not violated.

Taking into account all of the code, including that which you
can't see or know about, such as in the implementation of the
library.
Precisely.
In other words, it works just fine as long as you're lucky.
Not quite. Rather as long as one understands what he is doing.

--
Max
Nov 10 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2636
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
6
7384
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
9579
marktang
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...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
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...
0
6662
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();...
0
5293
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...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.