0% found this document useful (0 votes)
2 views5 pages

c++ string

Uploaded by

shubhamv08000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

c++ string

Uploaded by

shubhamv08000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/11/24, 8:57 PM C++ Cheat Sheet for Quick References (Download PDF)

User Inputs

C++ supports “cout” and “cin” for displaying outputs


and for taking inputs from users, respectively. The
cout uses the iteration operator (<<), and cin uses
(>>).
For example:

int x; // declaring a variable


cout << "Type a number: "; // Type any num
ber and hit enter
cin >> x; // Get user input from the keybo
ard
cout << "Your number is: " << x; // Displa
y the value

Strings

A string is a collection or sequence of characters


enclosed within double-quotes.
For example:

string str= "Hello";

To use string within your code, you must include the


string library using this code line:

#include <string>

C++ will then allow you to perform various functions


to manipulate strings. The following table describes
the function names and their descriptions:

Function Description

https://hackr.io/blog/cpp-cheat-sheet-pdf#strings 12/53
8/11/24, 8:57 PM C++ Cheat Sheet for Quick References (Download PDF)

int compare(const Compare two string


string& str) objects

Finds the length of the


int length()
string

Swaps the values of two


void swap(string& str)
string objects

string substr(int pos, int Creates a new string


n) object of n characters

Return the length of the


int size()
string in terms of bytes

Resizes the length of the


void resize(int n)
string up to n characters

Replaces the portion of


string& replace(int pos, the string beginning at
int len, string& str) character position pos
and spans len characters

Adds a new character


Login at
string& append(const
the end of another string
string& str)
object

Accesses an individual
char& at(int pos) character at specified
position pos

int find(string& str, int Finds a string specified


pos, int n) in the parameter

Find the first occurrence


int find_first_of(string&
of the specified
str, int pos, int n)
sequence

https://hackr.io/blog/cpp-cheat-sheet-pdf#strings 13/53
8/11/24, 8:57 PM C++ Cheat Sheet for Quick References (Download PDF)

Searches for the string


int for the first character
find_first_not_of(string& that does not match with
str, int pos, int n ) any of the characters
specified in the string

Searches for the string


int find_last_of(string&
for the last character of
str, int pos, int n)
a specified sequence

Searches for the last


int
character that does not
find_last_not_of(string&
match with the specified
str, int pos)
sequence

Inserts a new character


before the character
string& insert()
indicated by the position
pos

Finds the maximum


int max_size()
length of the string

Adds a new character ch


void push_back(char ch)
at the end of the string

Removes the last


void pop_back()
character of the string

Assigns new value to the


string& assign()
string

Copies the contents of


int copy(string& str)
string into another

Removes all the


void clear()
elements from the string

https://hackr.io/blog/cpp-cheat-sheet-pdf#strings 14/53
8/11/24, 8:57 PM C++ Cheat Sheet for Quick References (Download PDF)

const_reverse_iterator Points to the last


crbegin() character of the string

Copies the characters of


const_char* data()
string into an array

Checks whether the


bool empty()
string is empty or not

Removes the characters


string& erase()
as specified

Returns a reference of
char& front()
the first character

Appends a new
string& operator+=() character at the end of
the string

Assigns a new value to


string& operator=()
the string

Retrieves a character at
char operator[](pos)
specified position pos

Searches for the last


int rfind()
occurrence of the string

Refers to the last


iterator end()
character of the string

Points to the first


reverse_iterator rend()
character of the string

Reduces the capacity


void shrink_to_fit() and makes it equal to the
size of the string

https://hackr.io/blog/cpp-cheat-sheet-pdf#strings 15/53
8/11/24, 8:57 PM C++ Cheat Sheet for Quick References (Download PDF)

Returns pointer to an
array containing a null
char* c_str()
terminated sequence of
characters

Requests a change in
void reserve(inr len)
capacity

Returns the allocated


allocator_type
object associated with
get_allocator();
the string

Operators

C++ supports different types of operators to add


logic to your code and perform operations on
variables and their respective values. Here are the
C++ operator types:

1. Arithmetic Operators
You can perform common mathematical operations
with arithmetic operators.

Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

https://hackr.io/blog/cpp-cheat-sheet-pdf#strings 16/53

You might also like