Page 1 of 1
C++ Loop over array of unknown length
Posted: Sat Mar 25, 2006 2:38 pm
by Chris Corbyn
Apologies for the C++ spam I'm creating
Is this OK or could I do this a more sensible way?
Code: Select all
struct foo {
/* some things defined here */
};
foo someArray[] = {
{ /* values for first element */ },
{ /* values for next element */ },
/* repeat unknown number of times */
};
int main()
{
/* hackish loop? Does work though */
int i = 0;
while (true)
{
try { //Should just catch an exception if no such element
if (someArray[i].whateverValue)
{
/* do some stuff */
}
} catch (int e) {
break;
}
i++;
}
return 0;
}
Posted: Sat Mar 25, 2006 4:27 pm
by feyd
use an STL List or Queue.
Posted: Sat Mar 25, 2006 4:47 pm
by Chris Corbyn
feyd wrote:use an STL List or Queue.
OK time to read up on stl::list. Been mentioned a few times now.
Thanks

Posted: Sat Mar 25, 2006 6:38 pm
by Chris Corbyn

I don't get it. The tutorial I'm reading basically do this:
Code: Select all
#include <iostream>
#include <list>
using namespace std;
list<int> myList;
myList.push_back(0); //Gives error (??!)
int main()
{
return 0;
}
But when I try it I just get a token error:
Code: Select all
root @w3style /home/d11wtq/cpp# g++ test.cpp -o test.out
test.cpp:7: error: expected constructor, destructor, or type conversion before ‘.’ token
What have I missed?

Hmm... wish we had a n00b-friendly C++ forum
EDIT | One big advantage to stl::list (if I can figure it out) is that I can store my token defintions in a defintion file like this and read it at runtime rather than compile-time.
Code: Select all
# Name Pattern (PCRE) Modifiers
# -----------------------------------------------------------------
T_ESCAPE_CHAR \\
T_DOUBLE_STRING (?<!\\)".*?(?<!\\) s
T_LITERAL_STRING (?<!\\)'.*?(?<!\\)' s
T_COMMENT (?<!\\)/\*(.*?)\*/|/.*?$|#.*?$ sm
T_VARIABLE \$[a-z_]\w*\b i
T_OBJECT_OPERATOR ->
T_IS_IDENTICAL ===
T_IS_NOT_IDENTICAL !==
T_IS_EQUAL ==
T_IS_GREATER_OR_EQUAL >=
T_IS_LESS_OR_EQUAL <=
T_IS_NOT_EQUAL !=
T_PLUS_EQUAL \+=
T_MINUS_EQUAL -=
T_EQUALS =
T_PLUS \+
T_MINUS -
T_ELSEIF \belseif\b i
T_IF \bif\b i
T_ELSE \belse\b i
T_CLASS \bclass\b i
T_DOUBLE_COLON ::
T_COLON :
T_SEMICOLON ; i
T_LNUMBER 0x[a-f\d]+|\d+\.\d+|\d+ i
T_UNQUOTED_STRING \w+
T_WHITESPACE \s+
T_UNKNOWN \W
Posted: Sat Mar 25, 2006 6:42 pm
by feyd
you're trying to run code outside a function (not allowed)
Posted: Sat Mar 25, 2006 6:44 pm
by Chris Corbyn
feyd wrote:you're trying to run code outside a function (not allowed)
Ohhh....

I just assumed I could do it in the header file. My bad.
/swings back for round #2

Posted: Sun Mar 26, 2006 2:23 am
by timvw
Here are a couple of examples:
http://yolinux.com/TUTORIALS/LinuxTutorialC++STL.html.
Btw, i prefer to compile with all warnings, g++ -Wall source -o output.
For a simple program testprogram i've added the following to my .vimrc (compile with F9 and run with F10)
For larger projects there is always :make but that's for another time..
Code: Select all
map <F9> :!g++ -Wall ./%<cr>
map <F10> :!./a.out<cr>