Add character between lines in C ++

So, basically I am trying to add a character in the middle of the line. Usually in something like Python, this would be pretty straightforward, but I'm really not sure how to achieve this in C ++. What I am trying to achieve looks something like this:

void converter(){
    converted = ":regional_indicator_" + character + ":";

}

      

Basically, I am trying to add a variable of character

type char to a string. Should I store the character as a string instead?

For reference, here's all 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;
}

      

+3


source to share


4 answers


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, building std::string

from "character", you get const char *

+ std::string

, giving the string, and then std::string

+ const char *

again outputs the string as the final result.

+2


source


Add s

behind string literals to treat them like s std::string

instead const char*

:

converted = ":regional_indicator_"s + character + ":"s;

      



You will need to do either using namespace std::literals

or using namespace std::string_literals

for it to work.

On the side of the note, in C ++, it's weird to have a function converter()

to change a global variable using another global variable. Perhaps you should consider passing character

as a parameter to a function.

+3


source


You can avoid calling std :: string () constructor and memory allocation by using the following. I tested this before posting and it works:

void converter(){
    converted = ":regional_indicator_";
    converted.push_back(character);
    converted.push_back(':');
}

      

This is better because the "converted" already has spare memory, so you simply fill that additional memory with two more characters and you won't allocate new memory.

+1


source


With the wasy method, you can use std :: ostringstream like this:

void converter(){

    std::ostringstream oss;
    oss << ":regional_indicator_" << character << ":";

    converted = oss.str(); // copy the string out

    // ... etc ...
}

      

An additional advantage of this method is that it automatically converts numbers to strings.

This is 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 ...
}

      

This works because your string is of a fixed length. If thread safety is required, you can use thread_local static char* template...

.

0


source







All Articles