C++ for Swift Developers
Transfer Swift experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.
Swift and C++ side by side
The same program in Swift and in C++, from the lessons of this course.
Strings and Output
Swift String is a copy-on-write value whose count counts Characters (extended grapheme clusters); the UTF-8 byte count is a separate view, utf8.count. C++ string has no automatic UTF-8 validity guarantee. C++ std::string owns a mutable byte sequence; stream…
Swift1let name = "café"2print(name.count, name.utf8.count)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}Text Equality
Swift == compares String contents, while === asks whether two class references point to the same instance. C++ string supplies equality, while raw pointers still compare addresses. C++ std::string equality compares contents. Raw pointer equality compares…
Swift1final class Label {2 let text: String3 init(_ text: String) { self.text = text }4}5let a = Label("red")6let b = Label("red")7print(a.text == b.text)8print(a === b)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}Fixed-size Arrays
A Swift Array has no length in its type and can always grow, and every subscript is bounds-checked, trapping on a bad index. C++ array access with [] is not automatically bounds-checked; at checks and throws. C++ std::array<T,N> is a fixed-size value type.…
Swift1var a = [4, 5, 6]2let b = a3a[1] = 94print(a.count, b[1])C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}Value Copies
Swift arrays, strings and dictionaries are value types: var b = a gives b an independent value, and copy-on-write delays the actual buffer copy until one side mutates. Class instances are references, so two constants can share one object. C++ ordinary copy…
Swift1final class Box {2 var items: [Int] = [4]3}4 5var a = [4]6var b = a7b.append(9)8 9let p = Box()10let q = p11q.items.append(9)12print(a.count, b.count, p.items.count)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15}Functions and Returns
Swift functions declare the return type after ->, and a body that is a single expression returns it without the return keyword. Swift also warns when a call's result is ignored, unless the function is marked @discardableResult. C++ requires an explicit return…
Swift1func twice(_ value: Int) -> Int { value * 2 }2 3let result = twice(4)4print(result)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}Streams and Exact Parsing
Swift Int(text) returns an optional: nil unless the whole string is a valid integer, with no whitespace skipping and no partial prefix. C++ stream extraction instead skips leading whitespace and stops after the longest valid prefix. C++ stream extraction…
Swift1let n = Int("7") ?? -12let bad = Int("7x") ?? -13print(n, bad)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsC++ Files and Values
6 lessonsConditions and Iteration
5 lessonsStandard Containers
5 lessonsCopies and Algorithms
5 lessonsFunctions and Templates
5 lessonsParsing and Lifetime
5 lessonsObjects and Invariants
6 lessonsComposition and Ownership
5 lessonsProject: Library Desk
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is C++ for Swift Developers for?
For people who already write Swift and want to learn C++. Each lesson starts from Swift you know and shows how C++ does the same thing, so you skip the beginner basics.
Do I need to know C++ already?
No. The course starts with your first C++ program. You need Swift basics: variables, conditions, loops, functions and collections.
How many lessons does C++ for Swift Developers have?
C++ for Swift Developers contains 48 interactive lessons you can complete at your own pace.
What does C++ for Swift Developers cover?
The course is organized into chapters covering: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, and more.
Is C++ for Swift Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.