#define add(a,b,c) {c = a+b;}
int main()
{
int a = 1;
int b = 1;
int c;
add(a,b,c);
std::cout<<c;
return 0;
}
template <class _T>
_T add(_T a, _T b)
{
return a+b;
}
int main()
{
std::cout<<add<int>(1,1);
return 0;
}
You could have used using namespace std[cpp]#include <sstream>
#include <iostream>
template<typename _Tp>
std::string toString(const _Tp& __p)
{
std::stringstream ss;
ss << __p;
std::string ret;
ss >> ret;
return ret;
}
[/cpp]
ohh plz no plz no!... I totally dislike namespaces. It makes the code hard to follow when you're not familiar with it or doesn't have a good IDE. It's "ok" to use them on huge libraries like boost in order to save you a few keystrokes, for example:You could have used using namespace std
Well, thats actually the same as my first example (and I'm talking about the usage of preprocessor definitions) with the difference that it return a value... which at the end it's almost the same as mine. But yehLike Migxxx said but there's also another way:
Code:#define add(x, y) x+y
#include "iostream"
#include "cstdlib"
#include "string"
using namespace std;
struct db
{
int num;
string str;
};
int main ()
{
db p[10];
db *f = p;
int x = 0;
while (x<10)
{
cout << "What is his name?"<<endl;
getline(cin,f[x].str);
cout << "What is "<< f[x].str << "'s age?" <<endl;
cin>>f[x].num;
x++;
}
cout << "Name \t Age"<<endl;
for(int y = 0; y<10 ; y++)
{
cout<< f[y].str << "\t" << f[y].num << endl;
}
system("PAUSE");
return 0;
}