C++ Loop over array of unknown length

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

C++ Loop over array of unknown length

Post by Chris Corbyn »

Apologies for the C++ spam I'm creating :P

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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use an STL List or Queue.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:cry: 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're trying to run code outside a function (not allowed)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:you're trying to run code outside a function (not allowed)
Ohhh.... :oops: I just assumed I could do it in the header file. My bad.

/swings back for round #2 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>
Post Reply