c++ toupper - standard function? [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Convert a String In C++ To Upper Case
Hi,
I need a portable function to convert string in c++ to upper case. I'm now using toupper( char); function. Is it a standard function? If not, what it's the correct way to do it across platforms? Btw, is there any web / wiki where I can list all c++ standard functions? Thank you.

Yes, toupper is declared in the cctype header. You can transform a string with an algorithm:
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string str("hello there");
std::cout << str << '\n';
std::transform(str.begin(), str.end(), str.begin(), std::toupper);
std::cout << str << '\n';
}

For the latter question, there's http://www.cplusplus.com/.

Hi in our project we use boost/algorithm/string to_upper function project for windows and linux

Related

is there an easy way to get output of system commands into a string? [duplicate]

This question already has answers here:
c++: subprocess output to stdin
(2 answers)
popen equivalent in c++
(3 answers)
Closed 6 years ago.
Is there an easy way to get output of system commands into a string in C++?
Heres and example of what I mean, trying to get the epoch into a string.
It doesnt work of course.
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string t = system("date +%s");
cout << "Time " << t << endl;
return 0;
}
For that specific task you probably want to use time and ctime (or something similar).
For the more general case, see popen (or, on Microsoft compilers, _popen). This doesn't return a string directly; it returns a FILE *, which you can then read like you would a file.

Why did I get error by using strchr() in C++? [duplicate]

This question already has answers here:
C++ string equivalent for strrchr
(4 answers)
Closed 6 years ago.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
string a="asdasd";
if(!strchr(a,'a')) cout<<"yes";
return 0;
}
I just began to learn C++ programming and I don't know why I got error in this line
if(!strchr(a,'a')) cout<<"yes";
But if I tried to code it like this, it would run very well.
if(!strchr("asdasd",'a')) cout<<"yes";
I know it is a stupid question but I really don't know why.. sorry..
The library function strchr is for use with C-style strings, not the C++ string type.
When using std::string, the closest equivalent of strchr is find:
#include <iostream>
#include <string>
int main(){
std::string a="asdasd";
if(a.find('a') != std::string::npos) std::cout<<"yes";
}

What is the easiest way of reading a ASCII file into an C++ std::string? [duplicate]

This question already has answers here:
Read whole ASCII file into C++ std::string [duplicate]
(9 answers)
Closed 6 years ago.
Very often it is necessary to read a whole file content into an std::string.
Which is the easiest way of doing that?
Let's assume that we have a file "test.txt". Generally, the best way I find of doing that is using the following code:
#include <iostream>
#include <sstream>
#include <fstream>
int main()
{
std::ifstream ifs("test.txt");
std::stringstream buffer;
buffer << ifs.rdbuf();
std::cout << buffer.str() << std::endl;
return 0;
}

How to use C++11 std::stoi with gcc? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.

c++ string UTF-8 encoding [duplicate]

This question already has answers here:
How do I print UTF-8 from c++ console application on Windows
(8 answers)
Closed 9 years ago.
I'm new in c++, and I tried to write a very simple code, but the result is wrong, and I don't know how to fix it.
The code is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string test_string = "aáeéöôőüűč♥♦♣♠";
cout << test_string << endl;
return 0;
}
But the result is: a├íe├ę├Â├┤┼Ĺ├╝┼▒─ŹÔÖąÔÖŽÔÖúÔÖá
I am on Windows, using Code::Blocks.
Save file as UTF-8 without BOM signature, and try use printf().
//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>
int main() {
SetConsoleOutputCP(65001);
char test_string[] = "aáeéöôőüűč♥♦♣♠";
printf(test_string);
return 0;
}
And the result is: aáeéöôőüűč♥♦♣♠
Unfortunately working with UTF-8 on Windows is very problematic.
On Linux, you can simply wstring like this:
Does this code work universaly, or is it just my system?
But unfortunately Windows doesn't have an UTF-8 locale, so you are left with Windows API.
http://www.siao2.com/2007/01/03/1392379.aspx