-2

My question is that I have two arguments vector<int>& ivec and const int& numb. is there a way to eliminate const int& numb? I just want to do add 10 to all elements in the vector. Also I want to eliminate if part, since it's empty. Thanks in advance. Please do it recursively. My goal it to use as few arguments as possible in a recursive function.

#include <iostream>
#include <vector>
using namespace std;
vector<int> addten(vector<int>& ivec, const int& numb) {
  if (numb == 0) {

  } else { 
    ivec[numb - 1] += 10;
    addten(ivec, numb - 1);
  }
    return ivec;
}
ihm
  • 1,833
  • 2
  • 18
  • 23
  • 2
    Have a look at the book list and pick a beginners book. It's really worth it. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – pmr May 02 '12 at 21:00
  • Passing const references on native types doesn't really work out as well as larger classes. According to the man up top, anything more than a couple of words works best: http://www2.research.att.com/~bs/bs_faq2.html#call-by-reference – chris May 02 '12 at 21:01
  • "Please do it recursively." Can we assume this is an assigned project for a programming class? If so, please add the "homework" tag. – Robᵩ May 02 '12 at 21:04
  • @Robᵩ no it's not HW, it's just a practice I created for myself. Just to use as few arguments as possible in a recursive function. – ihm May 02 '12 at 21:06

1 Answers1

5

Your code feels very odd. The C++y way to do this would be:

std::vector<int> vec; // your vector
// adds 10 in place
std::transform(vec.begin(), vec.end(), vec.begin(), 
               std::bind(std::plus<int>(), _1, 10));
// adds 10 out-of-place 
std::vector<int> result;
std::transform(vec.begin(), vec.end(), std::back_inserter(result),
               std::bind(std::plus<int>(), _1, 10));

As you have specifically requested, I've implemented a very poor foldl in C++ that operates only on vector<T> instead of on iterators.

#include <vector>
#include <iostream>

// well, here comes C++ origami
template<typename Start, typename F, typename T>
Start foldl(Start s, F f, const std::vector<T>& v) {
  return foldl_impl(s, f, v, 0);
}

template<typename Start, typename F, typename T>
Start foldl_impl(Start s, F f, const std::vector<T>& v, 
                 typename std::vector<T>::size_type t) {
  if(t == v.size()) return s;
  typename std::vector<T>::size_type t2 = t++;
  return foldl_impl(f(s, v[t2]), f, v, t);
}


int main()
{
  std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7};

  std::vector<int> added = 
    foldl(std::vector<int>()
          , [](std::vector<int>& v, int i) { v.push_back(i+10); return v;}
          , vec);
  for(auto x : added) { 
    std::cout << x << std::endl;
  }

  return 0;
}

Please consider that this is far from good C++ style.

pmr
  • 58,701
  • 10
  • 113
  • 156
  • @ihm, do you have to? Recursion is generally a weaker solution. If this is homework, it should have the homework tag. – chris May 02 '12 at 21:03
  • @ihm Umh, of course you can implement a recursive `transform` but C++ and especially iterators are not really prone to it. – pmr May 02 '12 at 21:04
  • @ihm There you go. Don't use something like this. – pmr May 02 '12 at 21:19