c++ Initialising 2 different iterators in a for loop [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I declare variables of different types in the initialization of a for loop?
I'd like to have a for loop in c++ which constructs 2 different kinds of vector iterator in the initialisation.
Here is a rough idea of what I would like:
std::vector<double> dubVec;
std::vector<int> intVec;
double result = 0;
dubVec.push_back(3.14);
intVec.push_back(1);
typedef std::vector<int>::iterator intIter;
typedef std::vector<double>::iterator dubIter;
for (intIter i = intVec.begin(), dubIter j = dubVec.begin(); i != intVec.end(); ++i, ++j)
{
result += (*i) * (*j);
}
Anyone know what is the standard to do in this situation?
I can't just use a vector of double for the intVec because I'm looking for a general solution. [i.e. I might have some function f which takes int to double and then calculate f(*i) * (*j)]

You could declare a std::pair with first and second as the iterator types:
for (std::pair<intIter, dubIter> i(intVec.begin(), dubVec.begin());
i.first != intVec.end() /* && i.second != dubVec.end() */;
++i.first, ++i.second)
{
result += (*i.first) * (*i.second);
}

You can't declare variables of different types inside a for loop.
Just declare them outside:
intIter i = intVec.begin();
dubIter j = dubVec.begin();
for (; i != intVec.end(); ++i && ++j)
{
}

Check out the zip iterator. It does exactly what you want: parallel iterate over two or more sequences simultaneously. Using that, I'd write it as:
using namespace boost;
for (auto i=make_zip_iterator(make_tuple(dubVec.begin(), intVec.begin())),
ie=make_zip_iterator(make_tuple(dubVec.end(), intVec.end()));
i!=ie; ++i)
{
// ...
}
Admittedly, this get's a little more complicated if you don't have support for auto or other type inference in your specific case, but it can still be quite nice with a typedef.

For example
intIter i = intVec.begin();
dubIter j = dubVec.begin();
for (; i != intVec.end(); ++i && ++j)
{
result += (*i) * (*j);
}
you can declare several var. only of the same type in the for.
And are you sure with this part
++i && ++j
? I believe you want to write there
++i, ++j
So obviously you must read basics about for loop in C++

The easiest thing to do, at the expense of widening the scope of the iterators, would be to just hoist them up to the containing scope:
intIter i;
dubIter j;
for (i = intVec.begin(), j = dubVec.begin(); i != intVec.end(); ++i && ++j)
{
result += (*i) * (*j);
}

Don't overcomplicate things.
for( size_t i = 0; i < intVec.size(); ++i )
{
result += intVec[i] * dubVec[i];
}

It seems you need an inner_product algorithm.
#include <vector>
#include <functional>
#include <numeric>
#include <iostream>
struct my_plus
{
double operator()(int i, double d)
{
return d + i;
}
};
struct my_multiplies
{
double operator()(int i, double d)
{
return d * i;
}
};
int main()
{
std::vector<double> dubVec;
std::vector<int> intVec;
double result = 0;
dubVec.push_back(3.14);
intVec.push_back(1);
result = std::inner_product(intVec.begin(),
intVec.end(),
dubVec.begin(),
0.0,
my_plus(),
my_multiplies());
std::cout << result << std::endl;
}
I used my own functors, because I suspect the standard multiplies and plus expect both operands to be of similar type, but I might be wrong.

intIter i;
dubIter j;
for (i = intVec.begin(), j = dubVec.begin(); i != intVec.end() && j != dubIter.end(); ++i, ++j)
{
result += (*i) * (*j);
}

Related

How to convert an "int" variable to vector iterator in C++?

Imagine two nested loops each iterating with integer variables (let's say i and j). Inside these two loops, a function (fun) generates a vector (vec) using i and j as inputs. The generated vector has to be saved in a larger vector called total. I know that I can use push_back but I prefer to initialize my vector first (with static size). I tried to use insert but I don't know how can I convert int to vector<int>::iterator. I tried static_cast<vector<int>::iterator>(2*i +j) but it gives me this error:
no suitable constructor exists to convert from "int" to "__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int>>>"
using namespace std;
vector<double> total(M, 0); // Initilized with zeros
for (int i = 0; i < A; ++i)
{
for (int j = 0; j < B; ++j)
{
vector<double> vec = fun(i);
copy_n(vec.begin(), vec.size(), /* an iterator to where I want to save vec in total */);
}
}
The simplest method is vec.begin() + 2*i + j, but it'll work only for randomly accessible containers. For a generic solution you must use std::advance()
auto v = vec.begin();
std::advance(v, 2*i + j);
The return value of copy_n from the last iteration is the point where you should start inserting this iteration.
std::vector<double> total(M, 0); // Initilized with zeros
auto it = total.begin();
for (int i = 0; i < A; ++i)
{
for (int j = 0; j < B; ++j)
{
auto vec = fun(i);
it = std::copy_n(vec.begin(), vec.size(), it);
// or it = std::copy(vec.begin(), vec.end(), it);
}
}

Iterate over full array starting at any position, using only one variable

Fairly simple, I want to loop over every index of an array of arraysize using only one var for the loop. I have a way to do it with two vars i and j, where i tracks the actual index and loops around and j counts up to arraysize and terminates the loop:
for (unsigned int i = start, j = 0; //start is the starting index
j < arraysize;
++i == arraysize ? i = 0 : 0, ++j)
{
//do stuff
}
Is there some nifty way to do this with only i? Order doesn't matter, if backward iteration makes sense for some reason.
Clarification: I want to loop from start to arraysize - 1, then from 0 to start - 1.
At least as I understand it, you want to loop through the entire array, but you want to start from somewhere other than the beginning, then when you reach the end, you want to start back at the beginning and keep going until you reach the original starting point.
Assuming that's correct, it's pretty easy:
for (size_t i=0; i<arraysize; i++)
process(array[(i+start)%arraysize]);
I would prefer to abstract that algorithm into generic function (which would work even on things like std::forward_list), without doing superfluous modulo and addition operations (though, they may be acceptable in many cases):
#include <algorithm>
#include <iostream>
#include <iterator>
template<typename FwdIter, typename F>
F for_each_shifted(FwdIter first, FwdIter start, FwdIter last, F f)
{
using std::for_each;
return for_each(first, start, for_each(start, last, f));
}
int main()
{
using namespace std;
int v[] = { 1, 1, 2, 6, 24 };
for_each_shifted(begin(v), begin(v) + 3, end(v), [](int x)
{
cout << x << endl;
});
}
Output is:
6
24
1
1
2
Live Demo
for ( i=start; i<start+arraysize; i++ ) {
// do stuff with (i % arraysize) in place of i
}
for (size_t i = start; (i + 1) % arraysize != start: i = (i + 1) % arraysize) {
// stuff
}
This would get you there:
for (unsigned int i = start; i < start + arraySize; i++)
{
DoSomething(array[i % arraySize]);
}
Alternatively:
for (unsigned int i = 0; i < arraySize; i++)
{
DoSomething(array[(i + start) % arraySize]);
}
For example you can use the folloing loop statement
for ( int i = start; i < arraysize + start; i++ )
and inside the loop body instead of i use expression i % arraysize

Declaring a function that takes generic input and output iterators

I would like to modify this function so that mimics standard library algorithms by taking input iterators and writing to an output iterator instead of what it's currently doing.
Here is the code:
template <class T>
std::vector<std::vector<T>> find_combinations(std::vector<std::vector<T>> v) {
unsigned int n = 1;
for_each(v.begin(), v.end(), [&](std::vector<T> &a){ n *= a.size(); });
std::vector<std::vector<T>> combinations(n, std::vector<T>(v.size()));
for (unsigned int i = 1; i <= n; ++i) {
unsigned int rate = n;
for (unsigned int j = 0; j != v.size(); ++j) {
combinations[i-1][j] = v[j].front();
rate /= v[j].size();
if (i % rate == 0) std::rotate(v[j].begin(), v[j].begin() + 1, v[j].end());
}
}
return combinations;
}
How it's used:
std::vector<std::vector<int>> input = { { 1, 3 }, { 6, 8 } };
std::vector<std::vector<int>> result = find_combinations(input);
My problem is writing the declaration. I'm assuming that it involves iterator traits but I haven't been able to figure out the syntax.
First of all, don't pass vectors by value. A return value may be optimized and moved (even if it's nor c++11) , as an input parameter it's hard for the compiler to know if it can just pass a reference.
Second, you can't initialize a vector of vectors like that.
Now, for the syntax, just use:
std::vector<std::vector<T>> find_combinations(std::vector<std::vector<T>>& v) {
}
It will work fine.

C++ : Array no been initialized when size is determined in runtime

I have a problem with initializing an array of double with the size determined in runtime.
MyPoly MyPoly::operator *(const MyPoly& other)
{
int newDegree = this->_degree + other._degree;
double array [newDegree] ;
fillArrayWithZeros(array, this->_degree + other._degree);
PolyRep::iterator it1 = this->_poly->begin();
PolyRep::iterator it2 = other._poly->begin();
for (int i = 0; i <= this->_degree; ++i, ++it1)
{
for (int j = 0; j <= other._degree; ++j, ++it2)
{
array[i + j] += (*it1) * (*it2);
}
it2 = other._poly->begin();
}
return MyPoly(array, this->_degree + other._degree);
}
It's in the second line of the function. If ill put a number say - 10 it works just fine.
There is no compilation error and no runtime error, but when i debug the program i see the array is empty.
The thing is that in the following function the initialization works fine although the size of the array is being determined in runtime as well :
MyPoly MyPoly::operator +(const MyPoly& other)
{
int bigDegree = (this->_poly->getDegree() > other._poly->getDegree()) ?
this->_poly->getDegree() : other._poly->getDegree();
double arr [bigDegree];
PolyRep::iterator it1 = this->_poly->begin();
PolyRep::iterator it2 = other._poly->begin();
for (int i = 0; i <= this->_poly->getDegree(); ++i, ++it1)
{
arr[i] = *it1;
}
for (int i = 0; i <= other._poly->getDegree(); ++i, ++it2)
{
arr[i] += *it2;
}
return MyPoly(arr, bigDegree + 1);
}
Both function are in the same class.
Can someone explain what is the problem
In both codes, you are writing off the end of the array, which can cause arbitrarily bad behavior. You need to use < instead of <= in your loops, or allocate 2 extra slots.
To answer your question, you are otherwise using runtime-sized arrays correctly.
In one code, you're using ->_degree and in the other you're using ->getDegree(). Are those really the same thing?

Is there a way to define variables of two different types in a for loop initializer?

You can define 2 variables of the same type in a for loop:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
But it is illegal to define variables of different types:
int main() {
for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Is there a way to do this? (I don't need to use i inside the loop, just j.)
If you have totally hacked and obscure solution, It's OK for me.
In this contrived example I know you could just use double for both variables. I'm looking for a general answer.
Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach macro:
#define foreach(var, iter, instr) { \
typeof(iter) var##IT = iter; \
typeof(iter)::Element var = *var##IT; \
for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) { \
instr; \
} \
}
It can be used thus:
foreach(ii, collection, {
cout << ii;
}).
But I need something that will be used like that:
foreach(ii, collection)
cout << ii;
Please do not introduce any runtime overhead (but it might be slow to compile).
Please do not suggest to move any of
the variables outside of for body,
probably not usable for me as the
iterator has to disappear just after
the loop.
You could do this:
#include <iostream>
int main( int, char *[] ) {
{
float j = 0.0;
for ( int i = 0; i < 10; i += 1, j = 2*i ) {
std::cout << j << std::endl;
}
}
float j = 2.0; // works
std::cout << j << std::endl;
return 0;
}
Well, it's ugly. But you could use pair.
int main() {
for (std::pair<int,float> p(0,0.0f);
p.first < 10;
p.first += 1, p.second = 2*p.first) {
cout << p.second << endl;
}
}
Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see #kitchen's one above):
FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) {
}
The first part specifies a sequence of declarations: (a)(b).... The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.
There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:
if(int k = 0) ; else COMPOUND_STATEMENT
Then k is visible. Naturally, it always have to evaluate to false. So it can't be used by us. The other context is this one:
for(int k = 0; ...; ...) COMPOUND_STATEMENT
That's what i'm going to use here. We'll have to watch to only make one iteration of COMPOUND_STATEMENT. The actual for loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.
#include <boost/preprocessor.hpp>
#include <iostream>
#define EMIT_DEC_(R,D,DEC) \
for(DEC; !_k; )
#define FOR(DECS, COND, INC) \
if(bool _k = false) ; else \
BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS) \
for(_k = true; COND; INC)
int main() {
FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
std::cout << j << std::endl;
}
}
It's creating a bunch of for statements, each nested into another one. It expands into:
if(bool _k = false) ; else
for(int i = 0; !_k; )
for(float j = 0.0f; !_k; )
for(_k = true; i < 10; (i += 1, j = 2 * i)) {
std::cout << j << std::endl;
}
{
int i = 0;
float j = 0.0;
for ( ; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
The variables "disappear" after the block.
This will make the iterator (or in this case, float) disappear when it's no more needed:
int main() {
// some code...
{
float j = 0.0;
for (int i = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
// more code...
}
If you're having trouble with macros, there's a standard do..while trick that works perfectly:
#define MYFOR(init, test, post, body) \
do \
{ \
init \
for( ; test; post) \
body \
} while(0)
Use it as follows:
MYFOR( int i = 0; float j = 0.0f; , i < 10 , (i += 1, j = 2.0f * i),
{
cout << j << endl;
} );
It's ugly, but it does what you want: the scope of i and j is limited by the do..while loop from the macro, and it requires a semicolon at the end, so you won't get bitten by putting it in the predicate of an if/else statement.
This one is also ugly, but provides also some general way for declaring multiple variables with some given name and types in a for-loop
int main() {
for (struct { int i; float j; } x = { };
x.i < 10; x.i += 1, x.j = 2 * x.i) {
cout << x.j << endl;
}
}
EDIT: Question has changed once more. The question now explicitly wants to implement a foreach loop. The simplest answer:
#include <boost/foreach.hpp>
void( std::vector<int>& v ) {
BOOST_FOREACH( int & x, v ) {
x = x*2;
}
}
Injecting a variable into a code block
This is not intended as an answer, but to show a more general technique for injecting a variable into a code block. It seems as if the macro the OP is trying to define might use, even if it does incur in some overhead
There are a couple of places where you can define a variable with different scopes. You can define a variable inside any code block, and its lifespan will be to the end of that particular block. You can define a variable in the parenthesis of a for loop and the scope will be the loop block. You can also define a variable inside an if block and its scope will be that of the if (including the else clause).
You can combine those options above to create externally and inject variables into a code block without creating a variable whose lifespan exceeds that of the block. A practical example would be defining a foreach loop (simplified to work only on STL containers. The calling syntax would be:
void f( std::vector<int>& container )
{
INTVECTOR_FOREACH( int & x, container )
{
x = x*2;
}
}
With semantics similar to foreach in other languages: x gets referenced to each element in the container, so that the function actually doubles each value inside the integer vector.
Now the code of the simplified macro:
#define INTVECTOR_FOREACH( variable, container ) \
for ( std::vector<int>::iterator it = container.begin(); it!=container.end(); ++it ) \
if ( bool condition=false ) {} else \
for ( variable = *it; !condition; condition=true )
Generalizing the macro for any container and type requires some metaprogramming that falls out of the context of the question, but the idea of how it works (I hope) should not be too hard to follow.
The external for iterates over the container, in each iteration we execute another for only once defining the iterating variable (int & x in the sample code). We need a condition to control the number of iterations (1) of the internal loop, and that condition is injected with an if. We choose to make the if fail so that we can ensure that the user does not get unexpected results if she writes an else after the loop... macros are tricky.
Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop.
You could still do that, and put the whole thing in curly braces to make the extra variable go out of scope.
int main()
{
{
float j = 0.0;
for (int i = 0; i < 10; i += 1, j = 2*i)
{
cout << j << endl;
}
}
// more code...
}
This way j would go out of scope right after the loop.
With the requirements you give the simplest code I can think of is:
for ( int i = 0; i < 10; ++i )
{
float f = i * 2;
std::cout << f << std::endl;
}
You only use f as twice the value of i. The lifetime is limited to the loop and (at least in the simplified question you provide) floats are cheap to create (exactly as cheap as to assign).
If construction of the real float (I am assuming that since i is not really an int, f may not be a float either) is much more expensive than reassigning the value, then the other solutions of encapsulating inside an extra pair of curly braces to limit scope would be the best option.
int main() {
for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Maybe I'm being dense, but why do you even have to declare the float? You just "throw it away" when you leave the loop anyways. Right?
for(int i=0; i<10; ++i)
cout << (float)2*i << endl;
Why do you need j?
You say the i is your own type, and you just need to generate j out of i, right? Easy. Add a member function to i's class to generate the j value, and use that always. You can probably even make a macro to "hide" the call to that member function, if you want. :-)
Why don't you just declare and initialize your variables outside of the for loop? You can still test and increment as you have it now.