Posteriormente a consultar especialistas en este tema, programadores de deferentes áreas y maestros hemos dado con la solución al dilema y la compartimos en este post.
Ejemplo 1: cómo concatenar dos cadenas en C++
#include <iostream>
#include <cstdlib>
std::string text ="hello";
std::string moretext ="there";
std::string together = text + moretext;
std::cout<< together << std::endl;>> hello there
Ejemplo 2: concat string en C++
#include <iostream>
#include <sstream>intmain()
std::string value ="Hello, "+"world!";
std::cout<< value << std::endl;//Or you can use ostringstream and use integers too//For example:
std::ostringstream ss;
ss <<"This is an integer"<<104;
std::cout<< ss.str()<< std::endl;
Ejemplo 3: c++ string concatenación
string first_name ="foo"
string last_name ="bar"
std::cout<< first_name +" "+ last_name << std::endl;
Ejemplo 4: agregar string en C++
#include<iostream>
#include <string>intmain()//"Ever thing inside these double quotes becomes const char array"// std::string namee = "Caleb" +"Hello";//This will give error because adding const char array to const char array
std::string namee ="Caleb";
namee +=" Hello";//This will work because adding a ptr to a actual string
std::cout<< namee << std::endl;// output=>Caleb Hello//You can also use the below
std::string namee2 = std::string("Caleb")+" Hello";// This will work because constructor will convert const char array to string, adding a ptr to string
std::cout<< namee2 << std::endl;// output=>Caleb Hello
std::cin.get();
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)