0% found this document useful (0 votes)
7 views

C Methods

The document discusses various C++ data structures and their common methods. It covers vectors, strings, sets, maps, queues, stacks, and lists. For each, it lists common methods like insert, erase, size, empty, begin, end, front, back, and more. It provides examples of declaring the data structures and using some of their key methods.

Uploaded by

Amina Yahia
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)
7 views

C Methods

The document discusses various C++ data structures and their common methods. It covers vectors, strings, sets, maps, queues, stacks, and lists. For each, it lists common methods like insert, erase, size, empty, begin, end, front, back, and more. It provides examples of declaring the data structures and using some of their key methods.

Uploaded by

Amina Yahia
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/ 4

c++ methods

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#define FAST_IO (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
void solve(){
//code
}
int32_t main()
{
FAST_IO;
long long TC = 1;
cin >> TC;
while (TC--)
solve();
}

#include <vector>

vect<int> vec;

push_back(value): //Adds an element to the end of the vector.


pop_back(): //Removes the last element from the vector.
vec.erase(vec.begin() + 2); // Remove the element at index 2
reverse(vec.begin(), vec.end()); // Reverse the elements of the vector (function hathi ma traja3 chay )
size(): //Returns the number of elements in the vector.
empty(): //Checks if the vector is empty.
clear(): //Removes all elements from the vector.
resize(new_size): //Changes the size of the vector, optionally filling new elements with a default value.
reserve(new_capacity): //Increases the capacity of the vector to at least the specified amount.
capacity(): //Returns the current capacity of the vector.
at(index): //Accesses the element at the specified index with bounds checking.
operator[](index): // Accesses the element at the specified index without bounds checking.
front(): //Returns a reference to the first element of the vector.
back(): //Returns a reference to the last element of the vector.
begin(): // Returns an iterator to the beginning of the vector.
end(): //Returns an iterator to the end of the vector.
insert(position, value): //Inserts an element at the specified position.
erase(position): //Removes the element at the specified position.
erase(start, end): //Removes elements in the range [start, end).
swap(other_vector): //Swaps the contents of the vector with another vector.
emplace_back(args...): // Constructs and adds an element to the end of the vector.
emplace(position, args...): // Constructs and inserts an element at the specified position.
assign(count, value): // Replaces the contents of the vector with count copies of the specified value.
assign(start, end): // Replaces the contents of the vector with elements from the range [start, end).
data(): // Returns a pointer to the underlying array.
get_allocator(): // Returns the allocator associated with the vector.

// max and min element


auto minElement = std::min_element(vec.begin(), vec.end());
auto maxElement = std::max_element(vec.begin(), vec.end());
if (!vec.empty()) {
std::cout << "Minimum element of the vector: " << *minElement << std::endl;
std::cout << "Maximum element of the vector: " << *maxElement << std::endl;
} else {
std::cout << "Vector is empty!" << std::endl;
}
#include <string>

std::string myString = "Hello, world!";

length() or size(): //Returns the length of the string.


clear(): //Clears the contents of the string.
empty(): //Checks if the string is empty.
at(index): //Accesses the character at the specified index with bounds checking.
operator[](index): //Accesses the character at the specified index without bounds checking.
front(): //Returns the first character of the string.
back(): //Returns the last character of the string.
append(str): //Appends another string or a portion of a string to the current string.
push_back(char): //Appends a single character to the end of the string.
pop_back(): //Removes the last character from the string.
insert(pos, str): //Inserts another string or a portion of a string into the current string at the specified position.
erase(pos, len): // Erases a portion of the string starting at the specified position.
replace(pos, len, str): //Replaces a portion of the string starting at the specified position with another string or a portion of a
string.

substr(pos, len): // Returns a substring of the string starting at the specified position with the specified length.
find(str, pos): //Searches for the first occurrence of another string within the current string, starting from the
specified position
.
rfind(str, pos): //Searches for the last occurrence of another string within the current string, starting from the
specified position.
find_first_of(str, pos): //Searches for the first occurrence of any character in another string within the current string, starting
from the specified position.

find_last_of(str, pos): //Searches for the last occurrence of any character in another string within the current string, starting
from the specified position.

find_first_not_of(str, pos): // Searches for the first occurrence of any character not in another string within the current string,
starting from the specified position.
find_last_not_of(str, pos): //Searches for the last occurrence of any character not in another string within the current string,
starting from the specified position
.
compare(str): Compares the string with another string.

#include <set>

std::set<int> mySet = {5, 2, 8, 1, 9};

insert(val): Inserts a new element into the set.


erase(val): Removes the specified element from the set.
erase(iterator): Removes the element at the specified iterator from the set.
find(val): Searches the set for the specified element and returns an iterator to it if found, otherwise returns end().
begin(): Returns an iterator to the beginning of the set.
end(): Returns an iterator to the end of the set.
empty( ): Checks if the set is empty.
size(): Returns the number of elements in the set.
clear(): Clears all elements from the set.
count(val): Counts the number of elements equal to the specified value.
lower_bound(val): Returns an iterator to the first element in the set that is not less than the specified value.
upper_bound(val): Returns an iterator to the first element in the set that is greater than the specified value.
equal_range(val): Returns a pair of iterators representing the range of elements with a value that matches the specified value.
swap(other_set): Swaps the contents of this set with another set.

#include <map>

std::map<int, std::string> myMap;


// Insert elements into the map using insert() method
myMap.insert(std::make_pair(1, "apple"));
myMap.insert(std::make_pair(2, "banana"));
myMap.insert(std::make_pair(3, "orange"));
// Access elements in the map using []
std::cout << "Value corresponding to key 2: " << myMap[2] << std::endl;

insert(pair): Inserts a key-value pair into the map.


erase(key): Removes the element with the specified key from the map.
erase(iterator): Removes the element at the specified iterator from the map.
find(key): Searches the map for the specified key and returns an iterator to it if found, otherwise returns end().
begin(): Returns an iterator to the beginning of the map.
end(): Returns an iterator to the end of the map.
empty(): Checks if the map is empty.
size(): Returns the number of elements in the map.
clear(): Clears all elements from the map.
count(key): Counts the number of elements with the specified key.
lower_bound(key): Returns an iterator to the first element in the map whose key is not less than the specified key.
upper_bound(key): Returns an iterator to the first element in the map whose key is greater than the specified key.
equal_range(key): Returns a pair of iterators representing the range of elements with a key that matches the specified key.
swap(other_map): Swaps the contents of this map with another map.

#include <queue>

std::queue<int> myQueue;

push(val): Inserts a new element into the back of the queue.


pop(): Removes the front element from the queue.
front(): Returns a reference to the front element of the queue (the oldest element).
back(): Returns a reference to the back element of the queue (the newest element).
empty(): Checks if the queue is empty.
size(): Returns the number of elements in the queue.
swap(other_queue): Swaps the contents of this queue with another queue.

// output it
while (!myQueue.empty()) {
std::cout << myQueue.front() << " "; // Output the front element
myQueue.pop(); // Remove the front element
}

#include <stack>

std::stack<int> myStack;

push(val): Inserts a new element onto the top of the stack.


pop(): Removes the top element from the stack.
top(): Returns a reference to the top element of the stack (the newest element).
empty(): Checks if the stack is empty.
size(): Returns the number of elements in the stack.
swap(other_stack): Swaps the contents of this stack with another stack.

#include <list>

std::list<int> myList;

push_back(val): Inserts a new element at the end of the list.


push_front(val): Inserts a new element at the beginning of the list.
pop_back(): Removes the last element from the list.
pop_front(): Removes the first element from the list.
insert(iterator, val): Inserts a new element into the list at the specified position.
erase(iterator): Removes the element at the specified position from the list.
clear(): Removes all elements from the list.
size(): Returns the number of elements in the list.
empty(): Checks if the list is empty.
front(): Returns a reference to the first element in the list.
back(): Returns a reference to the last element in the list.
begin(): Returns an iterator to the beginning of the list.
end(): Returns an iterator to the end of the list.
rbegin(): Returns a reverse iterator to the beginning of the reversed list.
rend(): Returns a reverse iterator to the end of the reversed list.
sort(): Sorts the elements in the list.
merge(other_list): Merges another list into this list, both lists must be sorted.
splice(iterator, other_list): Transfers elements from another list into this list.
reverse(): Reverses the order of elements in the list.

#include <cmath>

Absolute Value: Finding the absolute value of a number.

C++: abs(), fabs(), std::abs()


Square Root: Calculating the square root of a number.

C++: sqrt(), std::sqrt()


Power: Raising a number to a power.

C++: pow(), std::pow()


Trigonometric Functions: Functions involving angles.

C++: sin(), cos(), tan(), asin(), acos(), atan(), atan2()


Logarithmic Functions: Computing logarithms.

C++: log(), log10(), log2()


Rounding: Rounding a floating-point number.

C++: round(), floor(), ceil()


Random Number Generation: Generating random numbers.

C++: rand(), std::rand(), <random> library


Modulo Operation: Finding the remainder of a division.

C++: % operator
Minimum and Maximum: Finding the minimum or maximum of two numbers.

C++: min(), max(), std::min(), std::max()


Comparisons: Checking if numbers are equal, greater than, or less than each other.

C++: ==, !=, <, <=, >, >=

You might also like