Archive
Replacement for CString::Format in standard C++
std::stringstream can be used as a replacement for CString::Format, if you are using CString just for the sake of Format, caveat being that it could be slow, I’ve heard a user mentioning this but not sure, I haven’t tested it out, but should be definitely better that using CString::Format and then assigning to std::string.
std::stringstream strm;
strm << "Age: " << nAge << ", DOB: " << szDate << ", Salary: " << nSalary;
std::cout << strm.str();[/sourcecode]
A TCHAR version of std stream and string classes
Quite simple…
// A TCHAR based std::string typedef std::basic_string<tchar> tstring; // A TCHAR based std::ifstream; typedef std::basic_ifstream</tchar><tchar , std::char_traits<TCHAR> > tstream; // A TCHAR based std::stringstream typedef std::basic_stringstream</tchar><tchar , std::char_traits<TCHAR>, std::allocator</tchar><tchar> > tstringstream;
So now no need to worry about UNICODE and ANSI, should work as CString, since TCHAR becomes char/wchar_t based on _UNICODE macro definition.
Also note that stl has provided UNICODE versions of these classes for e.g. wstring, wstringstream, wifstream, but since windows has a type that switches automagically between char/wchar_t, we are making use of it.
So the idea behind this is that stl classes are mostly template based, so this means you can add your own version of an stl class for a custom type just like I’ve done. As a conclusion we can say that std::string can be called a vector<char> but with dedicated string operations.
Renaming lengthy namespaces in C++
So if you have a namespace like std::tr1 you can rename it likewise
namespace tr1=std::tr1; // New name will be tr1 using namespace tr1; // Using this new name