V
V.Subramanian, India
Suppose I have a container, say vector, of element type
pair<int, string>. I want to populate the container with some values
through istream_iterator. Then I want to write back the values to cout/
file/ostringstream objects.
Consider the following program x.cpp:
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <iterator>
using namespace std;
typedef pair<int, string> pair_type;
inline void print(const pair_type& arg)
{
cout << arg.first << " " << arg.second << endl;
}
inline istream& operator>>(istream& in,
pair_type& arg)
{
int x;
string str;
if (in >> x >> str)
{
arg.first = x;
arg.second = str;
}
return in;
}
inline ostream& operator<<(ostream& out,
const pair_type& arg)
{
return out << arg.first << " " << arg.second << endl;
}
int main()
{
istream_iterator<pair_type> ii(cin);
istream_iterator<pair_type> eos;
vector<pair_type> v(ii, eos);
for_each(v.begin(), v.end(), print);
cout << "-----------------------------" << endl;
copy(v.begin(),
v.end(),
ostream_iterator<pair_type>(cout));
return EXIT_SUCCESS;
}
I compiled this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Even though I have provided overloaded input and output operators for
the 'pair_type', I get compilation error for populating the vector
through istream_iterator and copying through ostream_iterator. Since
the compilation error messages are so huge that I am unable to put
them here.
As expected there is no compilation error for the 'std::for_each()'
algorithm; but I do not want to use it to write values because 'cout'
is hard-wired into it. If I can somehow use 'std::copy()' algorithm,
then I can pass cout/ofstream/ostringstream objects to std::copy()
through ostream_iterator. Am I correct ?
Why doesn't the above program compile?
Suppose 'T' and 'U' are any two types. Suppose I have a container of
std:air<T, U>. Then what is done in real-world code to populate the
container(especially read each pair<T, U>) and write them to any
ostream/ofstream/ostringstream objects? Please provide the code. I am
asking the code for two reasons:1) my code doesn't compile. 2) I want
to know the clean solution used in real-world code.
Thanks
V.Subramanian
pair<int, string>. I want to populate the container with some values
through istream_iterator. Then I want to write back the values to cout/
file/ostringstream objects.
Consider the following program x.cpp:
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <iterator>
using namespace std;
typedef pair<int, string> pair_type;
inline void print(const pair_type& arg)
{
cout << arg.first << " " << arg.second << endl;
}
inline istream& operator>>(istream& in,
pair_type& arg)
{
int x;
string str;
if (in >> x >> str)
{
arg.first = x;
arg.second = str;
}
return in;
}
inline ostream& operator<<(ostream& out,
const pair_type& arg)
{
return out << arg.first << " " << arg.second << endl;
}
int main()
{
istream_iterator<pair_type> ii(cin);
istream_iterator<pair_type> eos;
vector<pair_type> v(ii, eos);
for_each(v.begin(), v.end(), print);
cout << "-----------------------------" << endl;
copy(v.begin(),
v.end(),
ostream_iterator<pair_type>(cout));
return EXIT_SUCCESS;
}
I compiled this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
Even though I have provided overloaded input and output operators for
the 'pair_type', I get compilation error for populating the vector
through istream_iterator and copying through ostream_iterator. Since
the compilation error messages are so huge that I am unable to put
them here.
As expected there is no compilation error for the 'std::for_each()'
algorithm; but I do not want to use it to write values because 'cout'
is hard-wired into it. If I can somehow use 'std::copy()' algorithm,
then I can pass cout/ofstream/ostringstream objects to std::copy()
through ostream_iterator. Am I correct ?
Why doesn't the above program compile?
Suppose 'T' and 'U' are any two types. Suppose I have a container of
std:air<T, U>. Then what is done in real-world code to populate the
container(especially read each pair<T, U>) and write them to any
ostream/ofstream/ostringstream objects? Please provide the code. I am
asking the code for two reasons:1) my code doesn't compile. 2) I want
to know the clean solution used in real-world code.
Thanks
V.Subramanian