Std::string character problem

std::string arrWords[10];
std::vector<std::string> hElemanlar;

…

this->hElemanlar.push_back(std::string(1, this->arrWords[sayKelime][j]));

…

What i am doing is: Every element of arrWord is a std::string. I get
the n th element of arrWord and then push them into hElemanlar.

Assuming arrWords[0] is “test”, then:

this->hElemanlar.push_back("t");
this->hElemanlar.push_back("e");
this->hElemanlar.push_back("s");
this->hElemanlar.push_back("t");

And my problem is although i have no encoding problems with arrWords, some utf-8 characters are not printed or treated well in hElemanlar.
How can i fix it?s

In C++, std::string is not utf-8 aware. A single C++ char of a std::string is not always a “real” utf-8 character, as a utf-8 character can spawn up to 6 C++ char.

You need to use utf-8 aware methods, see https://code.google.com/p/strutil/ for example.

When i test the code mentioned above, it works in Samsung Galaxy S4, Sony Experia M4 Aqua. But fails in Samsung Galaxy S3 and Samsung Tablet SM-T113 so far… Isn’t it strange?

The example chars: ö, ç, ğ, İ, ü, ş

std::setlocale(LC_ALL, "Turkish.UTF-8");

std::mbstate_t mb{};
int len;

                        

for(const char* p = &this->arrWords[sayKelime][0], *end = p + this->arrWords[sayKelime].size(); p < end; p += len ) {
    len = std::mbrlen(p, end - p, &mb);
    if(len < 0) break;
    this->hElemanlar.emplace_back(p, p + len);
}

log("locale ayarlandi 1: %s", setlocale(LC_ALL, ""));
log("locale ayarlandi 2: %s", setlocale(LC_ALL, NULL));
log("locale ayarlandi 3: %s", setlocale(LC_ALL, "tr_TR"));
log("locale ayarlandi 4: %s", setlocale(LC_ALL, "tr_TR.ISO-8859-9"));
log("locale ayarlandi 5: %s", setlocale(LC_ALL, "Turkish"));
log("locale ayarlandi 6: %s", setlocale(LC_ALL, "Turkish.UTF-8"));
log("locale ayarlandi 7: %s", setlocale(LC_ALL, "TURKISH"));
log("locale ayarlandi 8: %s", setlocale(LC_ALL, "Turkey"));

These logs all print null

Edit: And what i realized is that for some devices the code below works, for some not:

setlocale(LC_ALL, "");
log("locale ayarlandi 1: %s", setlocale(LC_ALL, ""));
setlocale(LC_ALL, NULL);
log("locale ayarlandi 2: %s", setlocale(LC_ALL, NULL));

If successful, log is “C”.