array / vector

Hi, I am trying to practice some c++ problems and someone suggested leetcode, which I am failing miserably at but thats not the point of this question. I have selected a few questions and they have said they contains arrays, but when I look at the code, (unless I am misreading it) there are no arrays but instead vectors. I have not covered vectors yet in my study of c++ so i do not know anything about them, so I am wondering are they just another array? I have posted one of the questions below and the code it gives. If I am maybe misunderstanding the code and it does contain an array, please let me know. I am really struggling with these problems, I barely understand what they are asking me to do.


Question

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

1
2
3
4
5
6
7
8
9
class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        for(i = 0; i < runningSum.length(); i++)
        {
            
        }
    }
};
vector<int> is analogous in many ways to an array of int. Similarly, vector<double> is analogous to an array of double.

See this tutorial on vectors:
https://cal-linux.com/tutorials/vectors.html
I guess that vectors probably go down as "arrays on steroids". They are basically the contiguous storage that arrays are, together with a whole plethora of functions to do clever things with them. Some nice things:
- you can address them exactly like normal arrays: nums[0], nums[1], etc ...
- they carry their size with them;
- they can be dynamically resized.

They are part of the standard template library, which is a powerful, and highly-optimised collection of containers.

However, for what you are doing you could actually just use
http://www.cplusplus.com/reference/numeric/partial_sum/

so I am wondering are they just another array?
Yes. For now, just ignore the weird angle bracket syntax and most extra functions that vectors have. Vectors can be used the same way arrays are used:

int arr[5] {};
is the same as
vector<int> arr(5);

arr[3] = 42;
is the same syntax for both cases.

1
2
3
4
5
void func(vector<int>& nums)
{
    int size = nums.size();
    // ...
}
is analogous to
1
2
3
4
void func(int nums[], int size)
{
    // ...
}
-- a vector retains its size information by nums.size(), so you don't need to pass a second parameter as the size.

Is runningSum the name of your function, or the name of your array/vector?
Last edited on
Thank you for the answers, and the explanations. I will check out the links provided. The book i am learning from is old, so i do not think it covers vectors, are they new? So the links will be useful.

Gando, that is all the code that was provided by the site to complete the problem, I think it is the name of the function though, but I could be mistaken.
Vectors are not new. They aren't as old as C++ itself, but C++ has been around since the '80s (before it was standardized), and the Standard Template Library (STL) was developed around 1993, which became part of the standard library once C++ was standardized in 1998.

But many books, especially older ones, still teach C++ as being "C with classes" and don't really teach the C++ standard library.

Here's an example of printing the partial sum of a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using std::vector;

int partial_sum(const vector<int>& nums, int max_index)
{
    int sum = 0;
    for (int i = 0; i < (int)nums.size() && i < max_index; i++)
    {
        sum += nums[i];
    }
    return sum;
}

int main()
{
    vector<int> vec(5); // = {1, 2, 3, 4, 5}
    for (int i = 0; i < 5; i++)
    {
        vec[i] = i + 1;
    }
    std::cout << partial_sum(vec, 3) << '\n';
}

6
Last edited on
Same program with some more standard library:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

int main()
{
    std::vector<int> vec(5);
    iota( begin(vec), end(vec), 1 );
    auto part = std::min( 3UL, vec.size() );
    std::cout << accumulate( begin(vec), begin(vec) + part, 0 ) << '\n';
}

http://www.cplusplus.com/reference/numeric/accumulate/
http://www.cplusplus.com/reference/numeric/iota/
http://www.cplusplus.com/reference/algorithm/min/

And again, without the vector:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <numeric>
#include <algorithm>

int main()
{
    constexpr size_t N {5};
    int arr[N] {};
    std::iota( std::begin(arr), std::end(arr), 1 );
    auto part = std::min( 3UL, N );
    std::cout << std::accumulate( arr, arr + part, 0 ) << '\n';
}
Last edited on
Topic archived. No new replies allowed.