473,769 Members | 2,214 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
rio wrote:
"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 seriously doubt the C standard says anything about the
relation between 'std::size-t' and 'std::vector<T> ::size_type'.
[...]
Schobi
Nov 10 '08 #21
rio wrote:
"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.com said:
#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();
why not write above only?
copy(v.begin(), v.end(), out_iter( out, " " ));
The "easy" solution outputs a trailing space.
is it not better the "operator<< " defined below?
....

No, it is not better.
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?
The vector is resizable, but does not resize itself on indexing. See
the push_back and resize methods.
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
The vector's destructor deallocates the memory. Destruction can be a
wonderful thing. :)
Nov 10 '08 #22
James Kanze wrote:
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.yego rush...@gmail.c omsaid:
>>>>> namespace std {
> template<class A1, class A2>
> ostream& operator<<(ostr eam& s, vector<A1, A2const& vec)
>>>>Which has undefined behavior.
>>>The correct alternative, AIUI:
>>>template<typ ename T>
std::ostream & operator<<(std: :ostream& out, std::vector<Tco nst& v) {
>>Correct in what sense?
>Correct in the sense that it implements the OP's intent, and
is allowed by the standard.
-- It doesn't work. Try it with std::vector<
std::vector< int , for example.
you don't output raw vectors, but vectors with a semantic
signification, which determines how you format them.
You're right. In real life, I don't write that sort of operator as a
template; I use a formatted_whate ver type. Good call.

My original intent was to provide a Standard-compliant alternative to
specializing a template in std for non-UDT types. Pete pointed out the
previous poster's UB, but did not suggest any alternative. There is a
delicate art to keeping such abstract discussions both generic and
focused, especially on Usenet.
Nov 10 '08 #23
On Nov 10, 6:29*am, "rio" <a...@b.cwrot e:
"rio" <a...@b.cha scritto nel
messaggionews:4 9************** *********@reade r5.news.tin.it. ..
"Kai-Uwe Bux" <jkherci...@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.
i know max(std::size_t ) >= max(vector::siz e_type)
If this is true, then g++, VC++ and both of the library
implementations used with Sun CC are buggy, since they all allow
me to instantiate std::vector with an allocator which typedef's
size_type to unsigned long long.
i know *v.size()-1<=max(vector:: size_type) <= max(std::size_t )
Given that v.size() returns a vector::size_ty pe, it's safe to
say that v.size() <= max( vector::size_ty pe ). But if
vector::size_ty pe is an unsigned long long, and size_t is only
an unsigned long or an unsigned int, the second part is false.

Depending on what you're doing, it may be acceptable to suppose
that the default allocator is being used---I use size_t for this
a lot, when I know the actual instantiation of vector. But it's
not acceptable in a generic library.

--
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 11 '08 #24
On Nov 10, 12:47*pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
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.
And has access to, and has read and understood all of the source
code of the standard library he's using, and can guarantee that
his code will never be compiled using a compiler where this is
not the case.

--
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 11 '08 #25
On Nov 11, 9:24*am, James Kanze <james.ka...@gm ail.comwrote:
On Nov 10, 12:47*pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
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.

And has access to, and has read and understood all of the source
code of the standard library he's using, and can guarantee that
his code will never be compiled using a compiler where this is
not the case.
You just confirmed that no luck involved here, this is just a question
of understanding. ;)

--
Max
Nov 11 '08 #26
Maxim Yegorushkin wrote:
On Nov 11, 9:24 am, James Kanze <james.ka...@gm ail.comwrote:
>On Nov 10, 12:47 pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.comwr ote:
>>On Nov 10, 10:22 am, James Kanze <james.ka...@gm ail.comwrote:
On Nov 10, 10:19 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.comwr ote:
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. [...]
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.
And has access to, and has read and understood all of the source
code of the standard library he's using, and can guarantee that
his code will never be compiled using a compiler where this is
not the case.

You just confirmed that no luck involved here, this is just a question
of understanding. ;)
During the last decade, I had to write and maintain code
that was compiled using half a dozen different compilers
(and compiler versions), multiplied by several std lib
implementations (and versions thereof) multiplied by half
a dozen platforms multiplied by several different apps
the code was used for (standalone app/plugin, GUI/command
line...). There were enough different things we had to
have a thorough understanding of to make the idea of
having to have a thorough understanding of each of those
implementations and code versions rather unattractive.

Having followed this whole battle of words, I wonder what's
wrong with putting this operator into the global namespace
and altogether avoiding the hassle of having to know about
things you're not supposed to know about?
Max
Schobi
Nov 11 '08 #27
On Nov 11, 11:20*am, Hendrik Schober <spamt...@gmx.d ewrote:

[]
* Having followed this whole battle of words, I wonder what's
* wrong with putting this operator into the global namespace
* and altogether avoiding the hassle of having to know about
* things you're not supposed to know about?
Nothing wrong and this is indeed the correct way to do so. I confused
this case with another one, sincere apologies.

--
Max
Nov 11 '08 #28
On Nov 11, 1:54*pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
On Nov 11, 11:20*am, Hendrik Schober <spamt...@gmx.d ewrote:

[]
* Having followed this whole battle of words, I wonder what's
* wrong with putting this operator into the global namespace
* and altogether avoiding the hassle of having to know about
* things you're not supposed to know about?

Nothing wrong and this is indeed the correct way to do so. I confused
this case with another one, sincere apologies.
This what I was confusing it with:

#include <map>
#include <iostream>
#include <iterator>

// should be in namespace std::
template<class T, class U>
std::ostream& operator<<(std: :ostream& s, std::pair<T, Uconst&
p)
{
return s << p.first << ' ' << p.second;
}

int main()
{
typedef std::map<int, intMap;
Map m;
std::copy(
m.begin()
, m.end()
, std::ostream_it erator<Map::val ue_type>(std::c out)
);
}

It won't compile unless operator<<(std: :ostream& s, std::pair<T, U>
const& p) is in namespace std.

--
Max
Nov 11 '08 #29
Maxim Yegorushkin wrote:
On Nov 11, 1:54 pm, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
>On Nov 11, 11:20 am, Hendrik Schober <spamt...@gmx.d ewrote:

[]
>> Having followed this whole battle of words, I wonder what's
wrong with putting this operator into the global namespace
and altogether avoiding the hassle of having to know about
things you're not supposed to know about?
Nothing wrong and this is indeed the correct way to do so. I confused
this case with another one, sincere apologies.

This what I was confusing it with:

#include <map>
#include <iostream>
#include <iterator>

// should be in namespace std::
template<class T, class U>
std::ostream& operator<<(std: :ostream& s, std::pair<T, Uconst& p)
{
return s << p.first << ' ' << p.second;
}

int main()
{
typedef std::map<int, intMap;
Map m;
std::copy(
m.begin()
, m.end()
, std::ostream_it erator<Map::val ue_type>(std::c out)
);
}

It won't compile unless operator<<(std: :ostream& s, std::pair<T, U>
const& p) is in namespace std.
I would have asked the same question for this code. :)
I don't understand why it doesn't compile. It comes down
to this
ostr << val;
with 'ostr' being an 'std::basic_ost ream<>' and 'val'
being an 'std::pair<>'. Why doesn't this find the global
operator?
Max
Schobi
Nov 11 '08 #30

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
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
10035
jinu1996
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...
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...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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

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.