Menu
CoddyTech
Course

C++ for R Developers

Transfer R experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.

R and C++ side by side

The same program in R and in C++, from the lessons of this course.

  1. Strings and Output

    R stores text in character vectors: "oak" is a vector of length 1, and nchar counts its characters. C++ std::string is one mutable byte sequence with explicit ownership. C++ std::string owns a mutable byte sequence; stream insertion formats values. Length…

    R
    1name <- "oak"2print(length(name))3print(nchar(name))
    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}
  2. Text Equality

    R == compares vectors element by element and returns a logical vector, while identical asks whether two whole objects are equal; R has no identity test for ordinary values. C++ operator behavior depends on the declared operand types. C++ std::string equality…

    R
    1a <- c("red", "blue")2b <- c("red", "green")3print(a == b)4print(identical(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}
  3. Fixed-size Arrays

    R vectors grow when you assign past the end, filling the gap with NA, and they copy on modify, so b <- a behaves as an independent copy. C++ std::array has a compile-time length. C++ std::array<T,N> is a fixed-size value type. Its size is part of its type…

    R
    1items <- c(4, 8)2items[4] <- 123print(items)4print(items[-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}
  4. Value Copies

    R uses copy-on-modify: after copy <- original both names share one value until one of them is changed, and then the changed name gets its own copy, nested lists included. Environments are the exception, since assigning one shares the same object. C++ vector…

    R
    1original <- list(c(1))2copy <- original3copy[[1]] <- c(copy[[1]], 2)4print(original[[1]])5e <- new.env()6e$n <- 17f <- e8f$n <- 29print(e$n)
    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}
  5. Functions and Returns

    R functions are values bound to names, the last evaluated expression is the return value, and arguments are checked only when the body uses them. At the top level R also prints any visible result for you. C++ function signatures are statically checked. C++…

    R
    1twice <- function(value) {2  value * 23}4twice(4)
    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}
  6. Streams and Exact Parsing

    R as.integer() turns invalid numeric text into NA with a warning instead of stopping, so unchecked bad input flows on as a missing value. C++ streams represent extraction failure in stream state. C++ stream extraction reports success through stream state.…

    R
    1parts <- strsplit("oak 7", " ")[[1]]2cat(parts[1], "/", as.integer(parts[2]), "\n", sep = "")3bad <- suppressWarnings(as.integer("seven"))4print(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 questions

More courses for R developers

Learn C++ from another language

Frequently asked questions

Who is C++ for R Developers for?

For people who already write R and want to learn C++. Each lesson starts from R 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 R basics: variables, conditions, loops, functions and collections.

How many lessons does C++ for R Developers have?

C++ for R Developers contains 48 interactive lessons you can complete at your own pace.

What does C++ for R 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 R Developers free?

Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.

Coddy programming languages illustration

Start C++ for R Developers for free

Get started