Sum of vector using iterators

Make sure that sum is initialised. If you compile it in cpp.sh it tells you that.
int sum = 0;
Why don't you do one thing at a time?

removerDuplicates doesn't work because on line 41:
data.resize(distance<vector<int>::const_iterator>(data.begin(), data.end()))
you simply keep the vector the same size.

isPresent won't work because it will return immediately on the basis of data[0].
You could just use find.

removeIf won't work because your iterator becomes invalid when you erase that element.


Last edited on
If you are going to just loop through the vector you can use a different type of for loop that avoids typing interators. Called "range-based for loops". They are a C++11 added feature.

Using range-based for loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include<vector>
#include<algorithm>
#include <cstdlib>
#include <ctime>

void fillVector(std::vector<int>& data)
{
   // static casting shuts up the warning about possible loss of data
   std::srand(static_cast<unsigned>(std::time(0)));

   for (size_t i = 0; i < 10; i++)
   {
      data.push_back(rand() % 10 + 1);
   }
}

void computeSum(const std::vector<int>& data)
{
   int sum = 0;

   for (const auto& itr : data)
   {
      sum += itr;
   }

   std::cout << "Getting sum of all: " << sum << '\n';
}

int main(void)
{
   std::vector<int> data;

   fillVector(data);

   std::cout << "vector is:\n";

   for (const auto& itr : data)
   {
      std::cout << itr << ' ';
   }
   std::cout << "\n\n";

   computeSum(data);
}

https://en.cppreference.com/w/cpp/language/range-for

Vectors hold their size, so you can declare a vector and when passed into a function retrieving the size (number of elements) is easy.

For instance, in main() you can declare your vector's size: std::vector<int> data(20);
And with a slight change to your fillVector() function you can fill a vector of any size (you can even use a range-based loop):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void fillVector(std::vector<int>& data)
{
   // static casting shuts up the warning about possible loss of data
   std::srand(static_cast<unsigned>(std::time(0)));

   for (size_t i = 0; i < data.size(); i++)
   {
      data[i] = rand() % 10 + 1;
   }

   // using a range-based loop
   /* 
   for (auto& itr : data)
   {
      itr = rand() % 10 + 1;
   }
   */
}
Last edited on
@Vm1234,

You ignored the rest of my post. Iterators become invalid when you delete their element. Google "erase-remove idiom"

Range-based for loops won't help you when deleting.

Irrelevant, but your isPresent() routine does nothing useful (see my last post) and isn't being used. Just use find() if you really need it.
Last edited on
Range-based for loops won't help you when deleting.

Yeah, iterators are needed for that.
you can do it with a loop. You should not, but you can if you want to turn one line into 5 every time you do it. I really dislike iterators, but forcing the issue is just too messy.
Last edited on
Registered users can post here. Sign in or register to post.