Skip to content

Instantly share code, notes, and snippets.

@Toyz
Last active August 29, 2015 14:27
Show Gist options
  • Save Toyz/5deb05e2e62a181c018d to your computer and use it in GitHub Desktop.
Save Toyz/5deb05e2e62a181c018d to your computer and use it in GitHub Desktop.
// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>
// comparison, not case sensitive.
bool compare_nocase (const std::string& first, const std::string& second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
return ( first.length() < second.length() );
}
int main ()
{
std::list<std::string> mylist;
std::list<std::string>::iterator it;
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three");
mylist.sort();
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
mylist.sort(compare_nocase);
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
TCHAR * text;
#ifdef UNICODE
/*/
// Simple C
const size_t size = ( wcslen(text) + 1 ) * sizeof(wchar_t);
wcstombs(&buffer[0], text, size);
std::vector<char> buffer(size);
/*/
// Windows API (I would use this)
std::vector<char> buffer;
int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
if (size > 0) {
buffer.resize(size);
WideCharToMultiByte(CP_UTF8, 0, text, -1, static_cast<BYTE*>(&buffer[0]), buffer.size(), NULL, NULL);
}
else {
// Error handling
}
//*/
std::string string(&buffer[0]);
#else
std::string string(text);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment