Answers for "cpp string"

C++
2

c++ string

#include <string>
#include <iostream>
#include <type_traits>
#include <cstring>

int main() {
  std::string str = "Hello, there";
  std::cout << std::boolalpha
  << str.capacity() << ", " << str.size() << ", " << std::strlen(str.data()) // 12, 12, 12
  << 'n' << std::is_same_v<std::string, std::basic_string<char>> // true
  << 'n' << str.front() + str.substr(1, 10) + str.back() // Hello there
  << 'n' << str[0] // H
  << 'n';
  
  str += "!"; 
  std::cout << str << 'n'; // Hello, there!
  str.erase(4, 4); // Hellhere!
  str.pop_back(); // Hellhere
  str.insert(4, " "); // Hell here
  std::cout << str << 'n'; // Hell here
  
}
Posted by: Guest on February-28-2021
3

string in cpp

// you want to include <string>
#include <string>
#include <iostream>

int main() 
{
  string helloWorld = "Hello World!"; // creating string and assigning
  std::cout << helloWorld;            // will output what you assigned it!
                                      /* you can also use strings with user 
                                      input (cin/getline)*/
  string namePerson{};
  getline(cin, namePerson); // getline allows for multi word input
  std::cout << namePerson;  // outputs name which person inputted
}
Posted by: Guest on October-20-2020
1

string c++

#include <string>
std::begin		| returns an iterator to the beginning of a container 
std::end		| returns an iterator to the end of a container 
std::size		| returns the length of string
std::to_string	| converts a number to string
std::stoi		| converts a string to a signed integer
std::getline 	| read data from an I/O stream into a string
std::swap  		| specializes the std::swap algorithm
std::empty 		| checks whether the container is empty
Posted by: Guest on June-05-2021
0

c++ write string

#include <iostream>

int main() {
	std::cout << "Hello" << std::endl; //endl = end line/new line
    // or
    printf("hello");
    
}
Posted by: Guest on March-02-2020

Browse Popular Code Answers by Language