Common Collections (Vector and Strings) in Rust [Notes]

Chapter 8: Common Collections These are my notes from the chapter-8 of rust book. Please scroll down to the bottom (Note) section if you are curious about what this is. 8.1: Storing Lists of Values with Vectors Vec<T> collection type discussed, aka vector: * By default contiguous. * All values should be of same type. // Creation let v: Vec<i32> = Vec::new(); // vec! macro for convenience // default integer type is i32 let v = vec![1, 2, 3]; // Modifying let mut v = Vec::new(); // Rust infers the type from the elements pushed here v.push(5); v.push(6); // ... // Dropping // a vector is freed, when it goes out of scope { let v = vec![1, 2, 3, 4]; // ... } // <-- v goes out of scope here, and hence memory is freed as well // Reading Elements of Vectors let v = vec![1, 2, 3, 4, 5]; // First way: let third: &i32 = &v[2]; println!("The third element is: {}", third); // Second way: match v.get(2) { Some(num) => println!("The third element is: {}", num), None => println!("There is no third element."), } .get(&index) method allows you to handle out of range errors. ...

January 9, 2022 · 8 min · Kushashwa Ravi Shrimali

Common Collections (Vector and Strings) in Rust [Notes]

Chapter 8: Common Collections (Hash Maps) In the previous blog, I shared my notes on strings and vectors in Rust, and in this post we’ll cover Hash Maps. I personally have found their use in competitive programming, a lot, but hopefully as we move on, we’ll see lots of use-cases in real-life problems. Hash Maps Hash Maps: HashMap<K, V> You can’t access using indices, but through keys. Hash Maps store data on heap. Hash Maps are homogenous (all keys must have same type, and all values must have same type). Use std::collections::HashMap to bring HashMap to scope. Creating a New Hash Map ...

January 9, 2022 · 5 min · Kushashwa Ravi Shrimali

Prefer const_iterators to iterators (Notes)

NOTE My notes on Chapter 3, Item 13 of Effective Modern C++ written by Scott Meyers. Some (or even all) of the text can be similar to what you see in the book, as these are notes: I’ve tried not to be unnecessarily creative with my words. :) In C++, iterators come handy to point at memory addresses of STL containers. For example, // C++11 std::vector<int> x {11, 9, 23, 6}; // begin() member function returns an iterator, which points to the first // memory address of the container x std::vector<int>::iterator it = x.begin(); While the general practice is to use const whenever possible, but programmers tend to use whenever it’s practical. const_iterators is particularly suggested when you want to use iterators, but you don’t need to modify what it points to. ...

September 26, 2021 · 5 min · Kushashwa Ravi Shrimali

Declaring Overriding Functions override (Notes)

NOTE My notes on Chapter 3, Item 12 of Effective Modern C++ written by Scott Meyers. Some (or even all) of the text can be similar to what you see in the book, as these are notes: I’ve tried not to be unnecessarily creative with my words. :) Overriding != Overloading Example of virtual function overriding: // Base class class Base { public: virtual void doWork(); // ... }; // Derived class from Base class Derived: public Base { public: // virtual is optional // this will "override" Base::doWork virtual void doWork(); // ... }; // This creates a "Base" class pointer to "Derived" class object std::unique_ptr<Base> upb = std::make_unique<Derived>(); // Derived doWork() function is invoked upb->doWork(); This is how virtual function overriding allows to invoke a “derived class function” from a base class interface. ...

September 25, 2021 · 4 min · Kushashwa Ravi Shrimali

Prefer Deleted Functions to Private Undefined Ones (Notes)

Prefer deleted functions to private undefined ones This item (11) in the chapter 3 focuses on: Why and How to prevent users calling particular functions? C++-98 and C++-11 approach What’s the difference between deleting a function vs declaring a member function private (and not defining them)? NOTE These are my notes on Chapter 3, Item 11 of Effective Modern C++ written by Scott Meyers. Some (or even all) of the text can be similar to what you see in the book, as these are notes: I’ve tried not to be unnecessarily creative with my words. :) ...

August 25, 2021 · 5 min · Kushashwa Ravi Shrimali

Prefer Scoped Enums over Unscoped Enums (Notes)

Scoped vs Unscoped Enums General rule: declaring a name inside curly braces is limited to that scope. Exception: C++-98 style Enums NOTE My notes on Chapter 3, Item 10 of Effective Modern C++ written by Scott Meyers. Some (or even all) of the text can be similar to what you see in the book, as these are notes: I’ve tried not to be unnecessarily creative with my words. :) ...

August 14, 2021 · 5 min · Kushashwa Ravi Shrimali

Prefer Alias Declarations to Typedefs (Notes)

One solution to avoiding using long type names: // So C++98 like typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPtrMapSS; ...

August 12, 2021 · 3 min · Kushashwa Ravi Shrimali

Releasing Docker Container and Binder for using Xeus-Cling, Libtorch and OpenCV in C++

Today, I am elated to share Docker image for OpenCV, Libtorch and Xeus-Cling. We’ll discuss how to use the dockerfile and binder. Before I move on, the credits for creating and maintaining Docker image goes to Vishwesh Ravi Shrimali. He has been working on some cool stuff, please do get in touch with him if you’re interested to know. First question in your mind would be, Why use Docker or Binder? The answer to it lies in the frequency of queries on the discussion forum of PyTorch and Stackoverflow on Installation of Libtorch with OpenCV in Windows/Linux/OSX. I’ve had nightmares setting up the Windows system myself for Libtorch and nothing could be better than using Docker. Read on, to know why. ...

September 15, 2020 · 3 min · Kushashwa Ravi Shrimali

Understanding how Vectors work in C++ (Part-2): What happens when you initialize a vector?

In the last blog post, I realized there were a lot of methods inherited from the base struct _Vector_base_ and _Vector_impl_data. Instead of directly going to the source code of these structs, I’ll go through their methods and objects by explaining what happens when we initialize a vector. That is, we will start from calling a vector constructor and then see how memory is allocated. If you haven’t looked at the previous blog post, please take a look here. I want to be thorough with the blog post, so I’ll divide this into multiple posts. By the end of this post, you’ll go through the following structs: ...

April 26, 2020 · 7 min · Kushashwa Ravi Shrimali

Understanding how Vectors work in C++ (Part-3): Diving deep into member functions of vectors

In this blog, we’ll continue diving deep into the source code of Vector Containers in GCC compiler. Today, we will be discussing some of the most commonly used methods of vectors, and how they are implemented. Before we start, if you haven’t looked at the previous blogs in the C++ series, please take a look here. If you are already familiar with memory allocation in vector containers and vector’s base structs, then you can skip reading the previous blogs and continue here. If not, I suggest you reading them. ...

April 26, 2020 · 7 min · Kushashwa Ravi Shrimali