473,779 Members | 2,023 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 4557
On Nov 11, 2:40*pm, Hendrik Schober <spamt...@gmx.d ewrote:
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?
Because expression "ostr << val" is template argument dependent and
thus is bound at the second phase of the two-phase name lookup. At the
second phase it uses ADL only to search for functions within
namespaces associated with ostr and val. ostr is std::basic_ostr eam
and val is std::pair<int, int>, thus one associated namespace is std.
int has no associated namespaces. So, the only namespace considered
for expression "ostr << val" is std, which lacks a suitable
operator<<().

--
Max
Nov 11 '08 #31
Maxim Yegorushkin wrote:
On Nov 11, 2:40 pm, Hendrik Schober <spamt...@gmx.d ewrote:
>Maxim Yegorushkin wrote:
[...]
>> #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?

Because expression "ostr << val" is template argument dependent and
thus is bound at the second phase of the two-phase name lookup. At the
second phase it uses ADL only to search for functions within
namespaces associated with ostr and val. ostr is std::basic_ostr eam
and val is std::pair<int, int>, thus one associated namespace is std.
int has no associated namespaces. So, the only namespace considered
for expression "ostr << val" is std, which lacks a suitable
operator<<().
But lookup isn't ADL only. The enclosing namespaces are considered,
too, aren't they? And the global namespaces is always enclosing.
(I'm not saying you're wrong. I just don't understand this.)
Max
Schobi
Nov 11 '08 #32
On Nov 11, 3:36*pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
On Nov 11, 2:40 pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
[...]
>* * #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?
Because expression "ostr << val" is template argument dependent and
thus is bound at the second phase of the two-phase name lookup. At the
second phase it uses ADL only to search for functions within
namespaces associated with ostr and val. ostr is std::basic_ostr eam
and val is std::pair<int, int>, thus one associated namespace is std.
int has no associated namespaces. So, the only namespace considered
for expression "ostr << val" is std, which lacks a suitable
operator<<().

* But lookup isn't ADL only. The enclosing namespaces are considered,
* too, aren't they? And the global namespaces is always enclosing.
* (I'm not saying you're wrong. I just don't understand this.)
At the second stage of the two-phase name lookup (at the point of
template instantiation) it is ADL only.
* Schobi
--
Max

Nov 11 '08 #33
Maxim Yegorushkin wrote:
On Nov 11, 3:36 pm, Hendrik Schober <spamt...@gmx.d ewrote:
>Maxim Yegorushkin wrote:
>>On Nov 11, 2:40 pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
[...]
#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?
Because expression "ostr << val" is template argument dependent and
thus is bound at the second phase of the two-phase name lookup. At the
second phase it uses ADL only to search for functions within
namespaces associated with ostr and val. ostr is std::basic_ostr eam
and val is std::pair<int, int>, thus one associated namespace is std.
int has no associated namespaces. So, the only namespace considered
for expression "ostr << val" is std, which lacks a suitable
operator<<( ).
But lookup isn't ADL only. The enclosing namespaces are considered,
too, aren't they? And the global namespaces is always enclosing.
(I'm not saying you're wrong. I just don't understand this.)

At the second stage of the two-phase name lookup (at the point of
template instantiation) it is ADL only.
I'm trying to come up with some trivial example that
illustrates this, but I fail. I have this

#include <iostream>

namespace N {
struct test { };

template< typename T >
void f(T) { std::cout << "f(T)\n"; }

void f(int) { std::cout << "f(int)\n"; }
}

template< typename T >
void g(T o) { f(o); }

int main()
{
N::test t;
g(t);
}

which compiles fine and gives the expected result.
What am I still missing.
Max
Schobi
Nov 11 '08 #34
On Nov 11, 4:39*pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
On Nov 11, 3:36 pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
On Nov 11, 2:40 pm, Hendrik Schober <spamt...@gmx.d ewrote:
Maxim Yegorushkin wrote:
[...]
* * #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?
Because expression "ostr << val" is template argument dependent and
thus is bound at the second phase of the two-phase name lookup. At the
second phase it uses ADL only to search for functions within
namespaces associated with ostr and val. ostr is std::basic_ostr eam
and val is std::pair<int, int>, thus one associated namespace is std.
int has no associated namespaces. So, the only namespace considered
for expression "ostr << val" is std, which lacks a suitable
operator<<() .
* But lookup isn't ADL only. The enclosing namespaces are considered,
* too, aren't they? And the global namespaces is always enclosing.
* (I'm not saying you're wrong. I just don't understand this.)
At the second stage of the two-phase name lookup (at the point of
template instantiation) it is ADL only.

* I'm trying to come up with some trivial example that
* illustrates this, but I fail. I have this

* * *#include <iostream>

* * *namespace N {
* * * * *struct test { };

* * * * *template< typename T >
* * * * *void f(T) { *std::cout << "f(T)\n"; }

* * * * *void f(int) { *std::cout << "f(int)\n"; }
* * *}

* * *template< typename T >
* * *void g(T o) { f(o); }

* * *int main()
* * *{
* * * * *N::test t;
* * * * *g(t);
* * *}

* which compiles fine and gives the expected result.
* What am I still missing.
Your example should work just fine.

Here is a simplified version of the problem with
std::ostream_it erator<std::pai r< and a global
operator<<(std: :ostream&, std::pair<>):

namespace N {

struct X {};

void bar(struct overload_for_co mpilers_with_no _two_phase_look up&);

template<class Tvoid foo(T t) { bar(t); }

}

template<class Tvoid bar(T);

int main()
{
N::X x;
foo(x);
}

--
Max
Nov 11 '08 #35
rio
"Hendrik Schober" <sp******@gmx.d eha scritto nel messaggio
news:gf******** **@hoshi.visyn. net...
rio wrote:
>"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49******* *************** *@read.cnntp.or g...
>>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'.
ok
what about relation between "size_t"
and 'std::vector<T> ::size_type'?
>[...]

Schobi

Nov 13 '08 #36
rio

"Jeff Schwab" <je**@schwabcen ter.comha scritto nel messaggio
news:2s******** *************** *******@giganew s.com...
rio wrote:
The vector's destructor deallocates the memory. Destruction can be a
wonderful thing. :)
yes i know it, but it is better to know where it is done

Nov 13 '08 #37
rio wrote:
"Hendrik Schober" <sp******@gmx.d eha scritto nel messaggio
news:gf******** **@hoshi.visyn. net...
>rio wrote:
>>"Kai-Uwe Bux" <jk********@gmx .netha scritto nel messaggio
news:49****** *************** **@read.cnntp.o rg...
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'.

ok
what about relation between "size_t"
and 'std::vector<T> ::size_type'?
It's not dealt with anywhere. As far as I know, all we know about
std::size_t is that it is unsigned and the return type of the sizeof()
operator. Whether it can hold any value that std::vector<T>: :size_type can
hold, I have not seen in the standard (neither the C standard, which says
nothing about vector<T>::size _type nor the C++ standard).
Best

Kai-Uwe Bux
Nov 13 '08 #38
rio wrote:
"Jeff Schwab" <je**@schwabcen ter.comha scritto nel messaggio
news:2s******** *************** *******@giganew s.com...
>rio wrote:
The vector's destructor deallocates the memory. Destruction can be a
wonderful thing. :)

yes i know it, but it is better to know where it is done
C++ destruction is deterministic, well-defined, and fairly easy to
understand. The fact that something is taken care of by a destructor
doesn't mean you don't know where it's done. If you use automatic
allocation, destruction happens when the object goes out of scope.
Nov 13 '08 #39
On Nov 13, 6:58*pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
rio wrote:
"Hendrik Schober" <spamt...@gmx.d eha scritto nel messaggio
news:gf******** **@hoshi.visyn. net...
rio wrote:
"Kai-Uwe Bux" <jkherci...@gmx .netha scritto nel messaggio
news:49****** *************** **@read.cnntp.o rg...
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'.
ok
what about relation between "size_t"
and 'std::vector<T> ::size_type'?
It's not dealt with anywhere. As far as I know, all we know
about std::size_t is that it is unsigned and the return type
of the sizeof() operator. Whether it can hold any value that
std::vector<T>: :size_type can hold, I have not seen in the
standard (neither the C standard, which says nothing about
vector<T>::size _type nor the C++ standard).
Well, vector<T>::size _type has to be an unsigned integral type,
capable of representing all of the positive values of
vector<T>::diff erence_type. And vector<T>::diff erence_type must
be the type returned by subtracting two iterators,

I'm almost sure that the intent was that vector<T>::size _type
should be then same as the size_type of it's allocator; this is,
at least, what all of the implementations I know do. Which
means that it can be pretty much anything the user wants (as
long as it is an unsigned integral type); the default allocator
is required to typedef it to size_t. (The current standard
states that it should be "a type that can represent the size of
the largest object in the allocation model." This is certainly
wrong, since it is incompatible with the requirement that it
must contain all of the positive values which the
difference_type can contain---I've used systems where the
largest single object was 65365, but where pointers were
effectively 20 bits, and the difference between two pointers
could be 1 MB.) All of this has been rewritten in the current
draft to be expressed in terms of concepts, so someone has a lot
of detailed reading to do between here and Jan. 15th (when I
have to submit my comments on the CD to AFNOR).

--
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 13 '08 #40

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
7385
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
10136
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...
1
10071
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,...
0
8958
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
7478
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
6723
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
5372
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
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
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...

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.