What is the purpose of locales?

What is the purpose of locales(the class). I managed to stumble upon this while I was looking for help on google. Couldn't get a straight forward definition on what this is? Let me put this into a context if it will help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  string convert(string &sentence)
{
	string convert;
	locale settings;
	for(int i = 0; i < sentence.size(); i++)
		convert += (toupper(sentence[i], settings));

	return convert;
}

int main()
{
	string input;
	bool running = false;  // Program isn't running
	

	while(!running)
	{
	  cout << "Enter a string(q to quit): ";
	  getline(cin, input);
	 
	  if(input == "q" || input == "Q")
	  {
	      cout << "Bye." << endl;
		  return 0;
	  }
		  
	   input = convert(input);
	   cout << input << endl;

	}


	
	return 0;
}

Thanks in advance.
I have not understood the question.
From the C++ Standard

This Clause describes components that C++ programs may use to encapsulate (and therefore be more portable when confronting) cultural differences. The locale facility includes internationalization support for character classification and string collation, numeric, monetary, and date/time formatting and parsing, and message retrieval.
locales perform conversions between strings and other data types (numbers, times, monetary units), conversions between character encodings (utf8, ucs4, gb18030, what have you), provide translations of messages into the user's language, classify and convert characters, etc. For the most part. they are used behind the scene by the IO streams library (cout << n; calls a locale facet to format a number as a string). It's a book-length topic

In this case, the author is using the current global C++ locale to convert characters to upper case. This is pointless because main() never changed the global, so it is no different from the default.
Thanks for the quick responses @vlad from moscow and @Cubbi. I have a better idea of what locales does now.

One more question: why is the following code legal?
 
convert += (toupper(sentence[i]));


the sentence variable was of type string and was not an array. How can the compiler allow this (sentence[i]) , while the user didn't define it as an array in the first place.
The string class overloads the subscript operator to allow you to treat it as if it were an array. Its implementation is probably similar to:
1
2
3
4
char& std::string::operator[](size_t index){
   if(index >= this->size()) throw std::out_of_range();
   return m_c_string[index]; //<-- Here I assume the actual string is a pointer to a C-string
}
Last edited on
@Daleth, ok thanks, now it makes sense. Thanks for the help.
Topic archived. No new replies allowed.