Slices in C++

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    // Unlike arrays, vectors are dynamically-sized sequences.
    // An uninitialized vector has size 0.
    std::vector<std::string> s;
    std::cout << "uninit: " << (s.empty() ? "true" : "false") << " " << (s.size() == 0 ? "true" : "false") << std::endl;

    // To create an empty vector with non-zero size, we can use the vector constructor.
    // Here we create a vector of strings with size 3 (initially empty strings).
    s = std::vector<std::string>(3);
    std::cout << "emp: [";
    for (const auto& str : s) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "] len: " << s.size() << " cap: " << s.capacity() << std::endl;

    // We can set and get just like with arrays.
    s[0] = "a";
    s[1] = "b";
    s[2] = "c";
    std::cout << "set: [";
    for (const auto& str : s) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;
    std::cout << "get: " << s[2] << std::endl;

    // size() returns the length of the vector as expected.
    std::cout << "len: " << s.size() << std::endl;

    // Vectors support adding elements using push_back.
    s.push_back("d");
    s.push_back("e");
    s.push_back("f");
    std::cout << "apd: [";
    for (const auto& str : s) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // Vectors can be copied using the assignment operator.
    std::vector<std::string> c = s;
    std::cout << "cpy: [";
    for (const auto& str : c) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // Vectors support a "slice" operator with the syntax vector.begin() + start, vector.begin() + end.
    auto l = std::vector<std::string>(s.begin() + 2, s.begin() + 5);
    std::cout << "sl1: [";
    for (const auto& str : l) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // This slices up to (but excluding) s[5].
    l = std::vector<std::string>(s.begin(), s.begin() + 5);
    std::cout << "sl2: [";
    for (const auto& str : l) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // And this slices up from (and including) s[2].
    l = std::vector<std::string>(s.begin() + 2, s.end());
    std::cout << "sl3: [";
    for (const auto& str : l) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // We can declare and initialize a vector in a single line as well.
    std::vector<std::string> t = {"g", "h", "i"};
    std::cout << "dcl: [";
    for (const auto& str : t) {
        std::cout << "\"" << str << "\" ";
    }
    std::cout << "]" << std::endl;

    // The <algorithm> library contains a number of useful utility functions for vectors.
    std::vector<std::string> t2 = {"g", "h", "i"};
    if (t == t2) {
        std::cout << "t == t2" << std::endl;
    }

    // Vectors can be composed into multi-dimensional data structures.
    std::vector<std::vector<int>> twoD(3);
    for (int i = 0; i < 3; i++) {
        int innerLen = i + 1;
        twoD[i] = std::vector<int>(innerLen);
        for (int j = 0; j < innerLen; j++) {
            twoD[i][j] = i + j;
        }
    }
    std::cout << "2d: [";
    for (const auto& inner : twoD) {
        std::cout << "[";
        for (int val : inner) {
            std::cout << val << " ";
        }
        std::cout << "] ";
    }
    std::cout << "]" << std::endl;

    return 0;
}

This C++ code demonstrates the usage of std::vector, which is similar to slices in Go. Here are some key points:

  1. We use std::vector instead of slices. Vectors in C++ are dynamically-sized sequences, similar to slices in Go.

  2. The append operation in Go is replaced by push_back in C++.

  3. Slicing in C++ is done using iterators. We use vector.begin() + start and vector.begin() + end to define the range.

  4. The copy function is not needed in C++ as we can directly assign vectors.

  5. The slices.Equal function from Go is replaced by the == operator in C++, which works for vectors of comparable types.

  6. Multi-dimensional vectors are created as vectors of vectors.

  7. We use range-based for loops to print vector contents, which is similar to Go’s for-range loops.

Note that while vectors are different from arrays, they are often used in C++ where you might use slices in Go. The std::vector provides similar functionality to Go’s slices, with dynamic sizing and easy append operations.