#include <string>
#include <sstream>
#include <iostream>
using namespace std;
struct Name_and_address {
string name;
string address;
Name_and_address(const char* p = "",const char* q = "")
: name(p),address(q) {}
};
ostream& operator<<(ostream& o,const Name_and_address& n)
{
return o << "(" << n.name << "," << n.address << ")";
}
istream& operator>>(istream& i,Name_and_address& n)
{
string s,t;
i >> s >> t;
n = Name_and_address(s.c_str(),t.c_str());
return i;
}
int main()
{
stringstream ss("Taro Tokyo");
Name_and_address n;
ss >> n;
cout << n << endl;
}
|
書いたやつ(<<)を読めない(>>)のは、我ながらセンス悪いね。(まぁいいか。)
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
struct Fstring : fstream {
class FProxy {
Fstring& m_f;
int m_i;
public:
FProxy(Fstring& f,int i) : m_f(f),m_i(i) {}
void operator=(char c){m_f.seekp(m_i); m_f.put(c);}
operator char()const{m_f.seekg(m_i); return m_f.get();}
};
Fstring(const char* p) : fstream(p) {}
FProxy operator[](int i){return FProxy(*this,i);}
// const FProxy バージョンは省略
};
int main()
{
Fstring f("test.txt");
f[2] = 'x';
string s;
s += f[1];
s += f[2];
s += f[3];
cout << s << endl;
}
|
#include <iostream>
using namespace std;
template <typename T1,typename T2,typename T3>
struct tri {
T1 first;
T2 second;
T3 third;
tri(T1 t1,T2 t2,T3 t3) : first(t1),second(t2),third(t3) {}
};
template <typename T,typename U>
ostream& operator<<(ostream& o,tri<ostream& (*)(ostream&,T,U),T,U>& t)
{return t.first(o,t.second,t.third);}
ostream& ShowDigit(ostream& o,int rad,int val)
{
char buf[80];
itoa(val,buf,rad);
return o << buf;
}
typedef ostream& (*FType)(ostream&,int,int);
tri<FType,int,int> based(int i,int j)
{return tri<FType,int,int>(ShowDigit,i,j);}
int main()
{
cout << based(2,9) << " " << based(3,19) << endl;
}
|
#include <iostream>
using namespace std;
class noecho_ostream {
friend ostream& echo(noecho_ostream o);
ostream* m_o;
public:
noecho_ostream(ostream& o) : m_o(&o) {}
template <typename T>
noecho_ostream operator<<(T t)const{return *this;}
};
noecho_ostream noecho(ostream& o){return o;}
noecho_ostream operator<<(ostream& o,
noecho_ostream (*f)(ostream&)){return f(o);};
ostream& echo(noecho_ostream o){return *o.m_o;}
ostream& operator<<(noecho_ostream o,
ostream& (*f)(noecho_ostream)){return f(o);};
int main()
{
cout << "abc" << noecho << "def" << echo << "xyz" << endl;
}
|
#include <iostream>
using namespace std;
char Rot1(char c)
{if (isalpha(c)) c += c == 'z' || c == 'Z' ? -25 : 1; return c;}
char Rot2(char c)
{if (isalpha(c)) c += c == 'a' || c == 'A' ? 25 : -1; return c;}
class crypt_ostream {
friend ostream& rawocrypt(crypt_ostream o);
ostream* m_o;
public:
crypt_ostream(ostream& o) : m_o(&o) {}
template <typename T>
crypt_ostream operator<<(T t){*m_o << t; return *this;}
crypt_ostream operator<<(char* p)
{while (*p) *m_o << Rot1(*p++); return *this;}
};
class crypt_istream {
friend istream& rawicrypt(crypt_istream i);
istream* m_i;
public:
crypt_istream(istream& i) : m_i(&i) {}
template <typename T>
crypt_istream operator>>(T t){*m_i >> t; return *this;}
crypt_istream operator>>(char* p0){char* p = p0;
*m_i >> p0; while (*p) *p++ = Rot2(*p); return *this;}
};
crypt_ostream encrypt(ostream& o){return o;}
crypt_ostream operator<<(ostream& o,
crypt_ostream (*f)(ostream&)){return f(o);};
ostream& rawocrypt(crypt_ostream o){return *o.m_o;}
ostream& operator<<(crypt_ostream o,
ostream& (*f)(crypt_ostream)){return f(o);};
crypt_istream decrypt(istream& i){return i;}
crypt_istream operator>>(istream& i,
crypt_istream (*f)(istream&)){return f(i);};
istream& rawicrypt(crypt_istream i){return *i.m_i;}
istream& operator>>(crypt_istream i,
istream& (*f)(crypt_istream)){return f(i);};
int main()
{
cout << encrypt << "apple\n" << rawocrypt << "apple\n";
char buf[80],buf2[80];
cin >> decrypt >> buf >> rawicrypt >> buf2;
cout << buf << endl << buf2 << endl;
}
|
#include <iostream>
using namespace std;
class ostrstream {
char* m_buf;
int m_len;
int m_pos;
public:
ostrstream(char* buf,int len) : m_buf(buf),m_len(len) {m_pos = 0;}
ostrstream& operator<<(char* p);
ostrstream& operator<<(int i);
};
ostrstream& ostrstream::operator<<(char* p)
{
int n = m_len - m_pos - 1, m = strlen(p);
if (n > m) n = m;
strncpy(m_buf+m_pos,p,n);
m_buf[m_pos += n] = 0;
return *this;
}
ostrstream& ostrstream::operator<<(int i)
{
char buf[80];
sprintf(buf,"%d",i);
return *this << buf;
}
int main()
{
char buf[80];
ostrstream o(buf,80);
o << 123; cout << buf << endl;
o << " abc"; cout << buf << endl;
}
|