Reputation: 181
So basically I'm trying to add a character in the middle of a string. Normally in something like Python, this would be pretty straightforward, but I'm really not sure how to achieve this in C++. What I'm trying to achieve is something like this:
void converter(){
converted = ":regional_indicator_" + character + ":";
}
So basically, I'm trying to add the variable character
of a type char in a string. Should I be storing character as a string instead?
For reference here's all of my code:
#include <iostream>
using namespace std;
string inputLine;
char character;
string converted;
void input(){
cout << "Please input the text in which you would like to be converted" << endl;
cin >> inputLine;
}
void converter(){
converted = ":regional_indicator_" + character + ":";
}
int main(){
input();
for (int i = 0; i < inputLine.length(); i++ ){
character = tolower(inputLine[i]);
}
return 0;
}
Upvotes: 3
Views: 1047
Reputation: 364
You can avoid invoking the std::string() constructor and memory allocation by using following. I have tested this before posting and it works:
void converter(){
converted = ":regional_indicator_";
converted.push_back(character);
converted.push_back(':');
}
It's better because "converted" already will have some extra memory reserved, so you will just be filling that extra memory with two more characters and won't be allocating new memory.
Upvotes: 1
Reputation: 48655
The wasy way to build strings is to use a std::ostringstream like this:
void converter(){
std::ostringstream oss;
oss << ":regional_indicator_" << character << ":";
converted = oss.str(); // copy the string out
// ... etc ...
}
The added advantage of that method is it converts numbers to string automatically too.
That's not the fastest way so if speed was important I would take advantage of the static nature of this concatenation like this:
std::string converter(){
static char* template = ":regional_indicator_X:";
template[20] = character; // replace the `X` with your character
converted.assign(template, 21); // assign your string all at once
// ... etc ...
}
That works because your string is of fixed length. If thread safety is required you can use thread_local static char* template...
.
Upvotes: 0
Reputation: 5690
Append s
behind the strings literals to treat them as std::string
s instead of const char*
s:
converted = ":regional_indicator_"s + character + ":"s;
You would need to do either using namespace std::literals
or using namespace std::string_literals
for it to work.
On a side note, in C++, it is strange to have a function converter()
to modify a global variable using another global variable. You might want to consider passing character
as a parameter to the function instead.
Upvotes: 3
Reputation: 31468
You can do it like this:
converted = ":regional_indicator_" + std::string(1, character) + ":";
This works because adding a string literal (const char *
) to a string yields a string. But adding const char *
and char results in pointer arithmetic. So, by constructing a std::string
from "character" you end up with const char *
+ std::string
yielding a string and then std::string
+ const char *
again yields a string as the final result.
Upvotes: 2