Looping through a <vector>

Discussion in 'C++' started by Some Clown, Apr 23, 2004.

  1. Some Clown

    Some Clown Guest

    Greetings,

    I'm trying to figure out how to loop through a vector of strings, searching
    each item as I go for either a boolean condition or a "contains" test. So
    if my vector is called 'v' I need to test v.0 for a boolean condition, then
    test v.1 and put the results in a new string, etc.

    I've tried several methods, none of which have worked. I've also been
    looking through my shiny "The C++ Programming Language" guide, but that's
    not so much of a "how-to" as a reference for those who already know. Kinda
    like looking in the dictionary to learn how to spell a word that you can't
    find because you don't know how to spell it.

    Here's the snippet I most recently tried, which obviously doesn't work:

    ------------------- snip ---------------------------
    for(i = 0; i < wholeCmdLine.size(); i++)
    {
    if(wholeCmdLine.find(".m3u"))
    {
    std::string playListFileName = wholeCmdLine;
    std::cout << playListFileName;
    }
    //else usage();
    }
    ------------------- snip ---------------------------
    Some Clown, Apr 23, 2004
    #1
    1. Advertising

  2. "Some Clown" <> wrote...
    > Greetings,
    >
    > I'm trying to figure out how to loop through a vector of strings,

    searching
    > each item as I go for either a boolean condition or a "contains" test. So
    > if my vector is called 'v' I need to test v.0 for a boolean condition,

    then
    > test v.1 and put the results in a new string, etc.
    >
    > I've tried several methods, none of which have worked. I've also been
    > looking through my shiny "The C++ Programming Language" guide, but that's
    > not so much of a "how-to" as a reference for those who already know.

    Kinda
    > like looking in the dictionary to learn how to spell a word that you can't
    > find because you don't know how to spell it.
    >
    > Here's the snippet I most recently tried, which obviously doesn't work:
    >
    > ------------------- snip ---------------------------
    > for(i = 0; i < wholeCmdLine.size(); i++)
    > {
    > if(wholeCmdLine.find(".m3u"))


    Change this to

    if (wholeCmdLine.find(".m3u") != std::string::npos)

    > {
    > std::string playListFileName = wholeCmdLine;
    > std::cout << playListFileName;
    > }
    > //else usage();
    > }
    > ------------------- snip ---------------------------
    >
    >


    Victor
    Victor Bazarov, Apr 23, 2004
    #2
    1. Advertising

  3. Some Clown

    David Harmon Guest

    On Thu, 22 Apr 2004 20:29:04 -0700 in comp.lang.c++, "Some Clown"
    <> wrote,
    >------------------- snip ---------------------------
    > for(i = 0; i < wholeCmdLine.size(); i++)
    > {
    > if(wholeCmdLine.find(".m3u"))


    string::find returns the position of the string if found,
    or npos if not found. So you might write

    if(wholeCmdLine.find(".m3u") != string::npos)
    David Harmon, Apr 23, 2004
    #3
  4. Some Clown

    Some Clown Guest

    "Victor Bazarov" <> wrote in message
    news:5f0ic.17248$GR.2501445@attbi_s01...
    > "Some Clown" <> wrote...
    > > Greetings,
    > >
    > > I'm trying to figure out how to loop through a vector of strings,

    > searching
    > > each item as I go for either a boolean condition or a "contains" test.

    So
    > > if my vector is called 'v' I need to test v.0 for a boolean condition,

    > then
    > > test v.1 and put the results in a new string, etc.
    > >
    > > I've tried several methods, none of which have worked. I've also been
    > > looking through my shiny "The C++ Programming Language" guide, but

    that's
    > > not so much of a "how-to" as a reference for those who already know.

    > Kinda
    > > like looking in the dictionary to learn how to spell a word that you

    can't
    > > find because you don't know how to spell it.
    > >
    > > Here's the snippet I most recently tried, which obviously doesn't work:
    > >
    > > ------------------- snip ---------------------------
    > > for(i = 0; i < wholeCmdLine.size(); i++)
    > > {
    > > if(wholeCmdLine.find(".m3u"))

    >
    > Change this to
    >
    > if (wholeCmdLine.find(".m3u") != std::string::npos)
    >
    > > {
    > > std::string playListFileName = wholeCmdLine;
    > > std::cout << playListFileName;
    > > }
    > > //else usage();
    > > }
    > > ------------------- snip ---------------------------


    Thanks a bunch, I'm not sure why I didn't figure that one out. :) While I
    was looking at my code however, I found something else odd. I have two
    loops, both for(...) loops using the standard 'int i = 0' one right after
    another. I get a multiple definition error however, so I had to change the
    second loop to 'int j = 0'. Doesnt' 'i' go out of scope when the loop is
    done?
    Some Clown, Apr 23, 2004
    #4
  5. >
    > Thanks a bunch, I'm not sure why I didn't figure that one out. :) While I
    > was looking at my code however, I found something else odd. I have two
    > loops, both for(...) loops using the standard 'int i = 0' one right after
    > another. I get a multiple definition error however, so I had to change

    the
    > second loop to 'int j = 0'. Doesnt' 'i' go out of scope when the loop is
    > done?
    >


    Some compilers don't follow that rule correctly, some have a compiler option
    to switch that rule on or off, which compiler are you using?

    A trick you can use to force your compiler to follow the correct rules is

    #define for if (0); else for

    Put that at the top of you code, and you force each for loop into the else
    part of an if ... else statement, therefore the proper scoping rules are
    followed. Obviously its better to use a compiler option if that's available
    though.

    john
    John Harrison, Apr 23, 2004
    #5
  6. Some Clown

    Some Clown Guest

    "John Harrison" <> wrote in message
    news:c6ad6n$9p4o3$-berlin.de...
    > >
    > > Thanks a bunch, I'm not sure why I didn't figure that one out. :) While

    I
    > > was looking at my code however, I found something else odd. I have two
    > > loops, both for(...) loops using the standard 'int i = 0' one right

    after
    > > another. I get a multiple definition error however, so I had to change

    > the
    > > second loop to 'int j = 0'. Doesnt' 'i' go out of scope when the loop

    is
    > > done?
    > >

    >
    > Some compilers don't follow that rule correctly, some have a compiler

    option
    > to switch that rule on or off, which compiler are you using?
    >
    > A trick you can use to force your compiler to follow the correct rules is
    >
    > #define for if (0); else for
    >
    > Put that at the top of you code, and you force each for loop into the else
    > part of an if ... else statement, therefore the proper scoping rules are
    > followed. Obviously its better to use a compiler option if that's

    available
    > though.


    At this point I'm using Microsoft Visual C++, although I'm planning on
    switching to the newest version soon (now that I get an academic discount).
    I hadn't thought to try my code on my linux box though... it might work fine
    under gcc. I'll try to look for a compiler option in the meantime.
    Some Clown, Apr 23, 2004
    #6
  7. Some Clown

    Jon Bell Guest

    In article <>,
    Some Clown <> wrote:
    >"John Harrison" <> wrote in message
    >news:c6ad6n$9p4o3$-berlin.de...
    >
    >> > [...] I have two
    >> > loops, both for(...) loops using the standard 'int i = 0' one right
    >> > after another. I get a multiple definition error however,

    >>
    >> Some compilers don't follow that rule correctly, some have a compiler
    >> >option to switch that rule on or off, which compiler are you using?

    >
    >At this point I'm using Microsoft Visual C++,


    That's a well known problem with VC++. As I recall reading, there's a
    switch to enable the standard for-loop scoping rule, which has the
    unfortunate side effect of breaking a bunch of MFC header files.

    >I hadn't thought to try my code on my linux box though... it might work fine
    >under gcc.


    Yes, g++ handles it just fine, except perhaps in really ancient versions.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Apr 23, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robk
    Replies:
    1
    Views:
    501
    Peter Jansson
    Mar 25, 2005
  2. pmatos
    Replies:
    6
    Views:
    23,727
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Aaron
    Replies:
    2
    Views:
    508
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    262
Loading...

Share This Page