Simple Version
I'm wondering if it's possible to define two variables (of different types) in the initialization section within a for loop. By this, I mean something like:
The above fails with an error "error: expected initializer before 'j'". Using a double instead of a string gives an error "error: unqualified-id before 'double'"Code:for(int i = 0, string j("a") ; i < 5 ; ++i) { // do something }
I know this is erroring because the comma operator isn't designed to deal with defining multiple types of variables in one statement.
Is there any way to accomplish this within the for loop syntax, without defining either variable outside or within the foor loop?
I don't want to use
And I don't want to useCode:string j("a"); for(int i = 0, i < 5 ; ++i) { // do something }
Code:for(int i = 0, i < 5 ; ++i) { string j("a"); // do something }
The "OK, what the heck are you trying to do?" version
OK, why don't I want to use either? Right now I'm using (simplified of course):
I'd like to remove the extra dereferencing to handle the iterator and go with something like:Code:#include <iostream> #include <vector> using namespace std; #define myfor(type, variableName, vectorName) for(vector<type>::iterator variableName = vectorName.begin() ; variableName != vectorName.end() ; ++variableName) int main() { vector<int*> vecOfInts; vecOfInts.push_back(new int(1)); vecOfInts.push_back(new int(2)); myfor(int*, theIntPointerIterator, vecOfInts) { // the define doesn't have the "{" character so can be used here for readability cout << **theIntPointerIterator << endl; } }
But, the above isn't valid as I mentioned a ways above, so won't compile.Code:#include <iostream> #include <vector> using namespace std; #define myfor(type, variableName, vectorName) for(vector<type>::iterator iter = vectorName.begin(), type variableName = *iter ; iter != vectorName.end() ; ++iter, variableName = *iter) int main() { vector<int*> vecOfInts; vecOfInts.push_back(new int(1)); vecOfInts.push_back(new int(2)); myfor(int*, theIntPointer, vecOfInts) { // the define doesn't have the "{" character so can be used here for readability cout << *theIntPointer << endl; } }
I don't want to define 'type variableName = *iter' before the for loop, becuase I don't want it accessable outside the for scope. Bad practice, and I'll be using multiple uses of the define macro, so unless I do something weird to work around it, I'd have multiple definitions of the same variable.
I don't want to define it inside the for loop, because then I have to include the "{" to enter scope within the define and my code becomes a less readable - and unusable by programming gui's:
(Edited typo)Code:#include <iostream> #include <vector> using namespace std; #define myfor(type, variableName, vectorName) for(vector<type>::iterator iter = vectorName.begin() ; iter != vectorName.end() ; ++iter) { \ type variableName = *iter; int main() { vector<int*> vecOfInts; vecOfInts.push_back(new int(1)); vecOfInts.push_back(new int(2)); myfor(int*, theIntPointer, vecOfInts) // note can't use "{" here becuase it's in the define cout << *theIntPointer << endl; } }




Reply With Quote