, , ; , - . , , . , , (). ; ostream, istream , .
#include <iostream>
#include <string>
#include <utility>
#include <tuple>
#include <vector>
template<class T>
std::ostream& operator<<( std::ostream& out, const std::vector<T>& v ) {
out << "{ ";
for( auto& a : v )
out << a << ' ';
out << '}';
return out;
}
template<class T>
std::istream& operator>>( std::istream& in, std::vector<T>& v ) {
int i;
std::string line;
std::getline( std::cin, line );
std::istringstream iss( line );
while( iss >> i ) {
v.push_back( i );
}
return in;
}
template<std::size_t> struct int_ {};
template<class Tuple, size_t Pos>
std::ostream& print_tuple( std::ostream& out, const Tuple& t, int_<Pos> ) {
out << std::get<std::tuple_size<Tuple>::value - Pos>( t ) << ' ';
return print_tuple( out, t, int_<Pos - 1>() );
}
template<class Tuple>
std::ostream& print_tuple( std::ostream& out, const Tuple& t, int_<1> ) {
return out << std::get<std::tuple_size<Tuple>::value - 1>( t );
}
template<class... Args>
std::ostream& operator<<( std::ostream& out, const std::tuple<Args...>& t ) {
return print_tuple( out, t, int_<sizeof...(Args)>() );
}
template<class Tuple, size_t Pos>
std::istream& write_tuple( std::istream& in, Tuple& t, int_<Pos> ) {
in >> std::get<std::tuple_size<Tuple>::value - Pos>( t );
return write_tuple( in, t, int_<Pos - 1>() );
}
template<class Tuple>
std::istream& write_tuple( std::istream& in, Tuple& t, int_<1> ) {
return in >> std::get<std::tuple_size<Tuple>::value - 1>( t );
}
template<class... Args>
std::istream& operator>>( std::istream& in, std::tuple<Args...>& t ) {
return write_tuple( in, t, int_<sizeof...(Args)>() );
}
template<class... T>
class StudentInfo;
template<class... T>
std::ostream& operator<< <>( std::ostream& out, const StudentInfo<T...>& c );
template<class... T>
std::istream& operator>> <>( std::istream& in, StudentInfo<T...>& c );
template<class...Args>
class StudentInfo {
public
std::tuple<Args...> members;
explicit StudentInfo(Args&&... args ) {
members = std::make_tuple<Args...>( std::move( args )... );
}
const StudentInfo<Args...>& operator() ( Args&&... args ) {
members = std::make_tuple<Args...>( std::forward<Args>( args )... );
return *this;
}
const StudentInfo<Args...> operator() ( Args&&... args ) const {
members = std::make_tuple<Args...>( std::forward<Args>( args )... );
return *this;
}
template<Args...>
friend std::ostream& operator<< <>(std::ostream& out, const StudentInfo<Args...>& c);
template<Args...>
friend std::istream& operator>> <>( std::istream& in, StudentInfo<Args...>& c );
StudentInfo<Args...>& operator=( StudentInfo<Args...>& c ) {
if ( members == c.members )
return *this;
members = c.members;
return *this;
}
};
template<class... T>
std::ostream& operator<< <>( std::ostream& out, StudentInfo<T...>& c ) {
return out << c.members;
}
template<class... T>
std::istream& operator>> <>( std::istream& in, StudentInfo<T...>& c ) {
return in >> c.members;
}
:
int main() {
std::string first{ "Some" };
std::string last{ "Day" };
int age = 1000;
std::string sex{ "Unknown" };
std::vector<int> grades{ 99, 98, 97, 92, 89, 88 };
StudentInfo< std::string, std::string, int,
std::string, std::vector<int> >
studentA( std::move(first), std::move(last),
std::move(age), std::move(sex),
std::move(grades)
);
std::cout << studentA << '\n';
first.clear();
last.clear();
age = 0;
sex.clear();
grades.clear();
StudentInfo< std::string, std::string, int,
std::string, std::vector<int> >
studentB( std::move(first), std::move(last),
std::move(age), std::move(sex),
std::move(grades)
);
std::cout << "Student B information\n" << studentB << '\n';
std::cout << "\nEnter the student information\n";
std::cin >> studentB;
std::cout << studentB << '\n';
StudentInfo< std::string, std::string, int,
std::string, std::vector<int> >
studentC( std::move( first ), std::move( last ),
std::move( age ), std::move( sex ),
std::move( grades )
);
std::cout << "Student C information\n" << studentC << '\n';
studentC = studentA;
std::cout << "Student C new information\n" << studentC << '\n';
studentC( std::move( std::get<0>( studentB.members ) ),
std::move( std::get<1>( studentB.members ) ),
std::move( std::get<2>( studentB.members ) ),
std::move( std::get<3>( studentB.members ) ),
std::move( std::get<4>( studentB.members ) )
);
std::cout << studentC << '\n';
std:cout << "\nPress any key and enter to quit.\n";
std::cin.get();
return 0;
}
, .
. , <T>. , , , std::vector<T>. , , , . ; vector, iostream , , . .