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());
Related
This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 months ago.
std::string sszModName = "kernel32.dll";
std::string WinVersion = "WIN81";
std::string MachineGUID= "ce9e95db-5fda-436a-b29a-f5537702c77d";
char buf[1024];
sprintf(buf, "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", sszModName, "ERROR_HASH_VERIFY", WinVersion, MachineGUID);
This code causes program lag, could you help me figure out why?
Try
sprintf(buf,
"https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s",
sszModName.c_str(),
"ERROR_HASH_VERIFY",
WinVersion.c_str(),
MachineGUID.c_str());
C strings are not the same as C++ strings. spprintf only uses C strings so you must use .c_str() to turn your C++ strings into C strings.
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:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).
This question already has answers here:
Get the last element of a std::string
(4 answers)
Closed 7 years ago.
In python you can say print "String"[-1] and it would print be the last character, 'g'. Is there an equivalent for this in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
Use the back() function for std::string:
std::string str ("Some string");
cout << str.back()
Output:
g
For C strings, it is
String[strlen(String) - 1];
For C++ style strings, it is either
String.back();
*String.rbegin();
String[String.length() - 1];
You can use the function:
my_string.back();
If you want to output it, then:
#include <iostream>
std::cout << my_string.back();