Transitioning from C to C++: Basic Concepts

Written on 2025.8.25 Personal Obsidian library, archived to public account.

Universal Header File

Standard: #include<iostream>

> #include<bits/stdc++.h>

>using namespace std;

Input and Output

Input cin >>

Output cout <<

String

string (6, ‘A’); #Repeat ‘A’ six times

> Access:

> for (int i = 0; i < s.size(); i++)

> cout << s[i];

Call:

> cout << s.substr(a, b) << endl;

#a: index, b: length

Replace:

> s.replace(2, 5, “XXXXX”);

#Replace characters from index 2 to 5 with XXXXX

Delete:

> s.erase(2, 5);

Convert to character array:

> printf(“%s\n”, s.c_str());

> Reverse reverse(s.begin(), s.end())

Vector

Defines a dynamic array, no need to specify size, equivalent to C’s int arr[]

Requires <vector> header file

> vector<int> A;

> A.push_back(1);

> cout << A[0] << endl;

> cout << A.size() << endl;

Iterator traversal:

> for(auto i = A.begin(); i != A.end(); i++){

> cout << *i << endl;

> }

> #auto = vector<int>::iterator

> #A.end points to the position after the last element

Set

Elements are sorted in ascending order, no duplicate elements

> set<int> s;

> s.insert(1);

> s.insert(2);

> s.insert(3);

> if (s.find(5) != s.end()){

> cout << “YES” << endl;

> }

Map

Maps one thing to another

Automatically sorts all keys in ascending order.

> map<string, int> m;{

> {“zhl”, 60}{“zzz”, 70}

> }

> #m[“zhl”] = 60;

> Iterate:

> for(auto i = m.begin(); i != m.end(); i++){

> cout << i->first << endl;

> cout << i->second << endl;

> }

Stack

Queue

Functions

1. sort

Function from the algorithm header, mainly for sorting arrays

2. lower_bound

Binary search for the address of the first element that is greater than or equal to a given element, must be sorted before use

3. upper_bound

Leave a Comment