⚡0 XP🔥0

std::string and std::vector

Lesson 3 of 12 ⏱ 6 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs C++

Work with C++ strings (length, substr, find, replace, compare) and vectors - dynamic arrays with push_back, size, range-for loops, 2D vectors and sorting.

std::string#

std::string (from <string>) manages its own memory: it grows as needed, knows its length and supports +, == and < directly.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Data Science
Length: 12
First char: D, last: e
substr(5, 7): Science
find("Sci"): 5
Computer Science
B.Tech Computer Science
Computer Science
Operation Example Result
length s.length() or s.size() number of characters
join a + b, a += b concatenation
compare a == b, a < b dictionary order
part s.substr(pos, len) sub-string
search s.find("x") index or string::npos
change replace, insert, erase modify in place
convert to_string(42), stoi("42"), stod("3.5") number ↔ text
▶ Try it YourselfC++
Ctrl + Enter to run
Output
apple comes first
VEDU-1043, fee/2 = 22999.8
User: asha, domain: vindhya.edu

std::vector - a dynamic array#

A vector<T> (from <vector>) is an array that grows and shrinks by itself and knows its own size. It is the most-used container in C++.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Size: 5
First: 72, last: 57
72 90 65 91 
Is empty? false
हिंदी में समझें

vector एक ऐसा array है जो ज़रूरत के हिसाब से अपने आप बड़ा-छोटा हो जाता है। push_back से आखिर में element जुड़ता है, size() से गिनती मिलती है और for (int m : marks) से हर element पर आसानी से loop चलता है। C की तरह malloc/free की चिंता नहीं करनी पड़ती।

Reading input into a vector, and sorting#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Sorted: 12 21 33 45 67 89 90
Max: 90
Top 3: 90 89 67
Note

v.begin() and v.end() are iterators - positions that mark the start and one-past-the-end of the data. Most STL algorithms take such a range.

Bounds checking with at()#

v[i] does no checking (like a C array). v.at(i) checks and throws an exception for a bad index:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
2
Error: index out of range

Vectors of strings and 2D vectors#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
1. Kabir
2. Aditi
3. Rohan
4. Meera
1	2	3	4	
2	4	6	8	
3	6	9	12	

Summary#

  • std::string grows automatically and supports +, ==, substr, find, replace, to_string, stoi.
  • std::vector<T> is a resizable array: push_back, pop_back, size, front, back, at.
  • Use range-for loops (for (int x : v)) and sort(v.begin(), v.end()).
  • vector<vector<int>> makes a 2D grid.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Word frequency of a letter

Read a line and a character, then print how many times the character occurs (case-insensitive) as 'a' occurs 4 times.

Test input:
A banana a day keeps worries away
a
Expected output:
'a' occurs 8 times
2

Above-average marks

Read n and then n marks into a vector<int>. Print the average with 1 decimal and then the marks that are above average:

Average: 68.0
Above average: 75 91 80
Test input:
6
55 75 91 48 59 80
Expected output:
Average: 68.0
Above average: 75 91 80

Quick Quiz

Q1.

What does string s = "Indore"; s.substr(1, 3) return?

Q2.

What does v.push_back(7) do for a vector<int> v?

Q3.

What does s.find("x") return if "x" is not in s?

Q4.

Which is the safest way to access element i of a vector?