This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 7 years ago.
So basically what I'm trying to find out, take this scenario:
std::string input;
Ask user for a string?: Apples
cin >> input;
std::string Apples = "input";
So basically, I ask the user for a string, and then create a variable with name of that string. Is this possible?
No, you can't do that.
The closest functionality is to use a map.
std::map<std::string, int> aMap;
Related
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded filepath, like this std::rename(filepath, destination);?
I don't know how you want to populate the strings in question, but here you go:
std::string fromName {"whatever you're going to do"};
std::string toName {"whatever you're going to do"};
std::rename(fromName.c_str(), toName.c_str());
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I want to create sth like:
int main ()
{
string s{};
std::cout << "Enter CMD: \n";
getline(cin,s);
system(s);
}
But since I can use only const char on system, its not working at all, is there any different solution to this? mabye shellexecute?
You can use std::string::c_str().
system(s.c_str());
This question already has answers here:
std::map<>::insert using non-copyable objects and uniform initialization
(2 answers)
Handling map of files in c++
(1 answer)
Closed 5 years ago.
I want to create a map of a key and the corresponding file in C++. I used the below snipper, which is giving mean exception at last line *m_jsTabFilesMap[key] << text;:
std::map<std::string, std::ofstream*> m_jsTabFilesMap;
std::string text = "hello all";
std::string key = "a";
*m_jsTabFilesMap[key] << text;
The copy constructor of std::ofstream is explicitely deleted. You have to store the streams by pointer or in a different way that doesn't try to copy the stream object if you want them in the map.
This question already has answers here:
How to copy std::string into std::vector<char>? [duplicate]
(2 answers)
Closed 7 years ago.
How to convert a string such as "Hello, world!" into a vector of characters? I've seen many techniques on how to do this for plain arrays, but none for vectors. Here is my code:
string raw_text = "Hello, world!";
vector<char> char_text;
Any way to make a character vector from raw_text?
Thanks in advance!
vector<char> char_text(raw_text.begin(), raw_text.end());