Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Looping through a <vector>

Reply
Thread Tools

Looping through a <vector>

 
 
Some Clown
Guest
Posts: n/a
 
      04-23-2004
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[i].find(".m3u"))
{
std::string playListFileName = wholeCmdLine[i];
std::cout << playListFileName;
}
//else usage();
}
------------------- snip ---------------------------


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-23-2004
"Some Clown" <(E-Mail Removed)> 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[i].find(".m3u"))


Change this to

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

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


Victor


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      04-23-2004
On Thu, 22 Apr 2004 20:29:04 -0700 in comp.lang.c++, "Some Clown"
<(E-Mail Removed)> wrote,
>------------------- snip ---------------------------
> for(i = 0; i < wholeCmdLine.size(); i++)
> {
> if(wholeCmdLine[i].find(".m3u"))


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

if(wholeCmdLine[i].find(".m3u") != string::npos)


 
Reply With Quote
 
Some Clown
Guest
Posts: n/a
 
      04-23-2004
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:5f0ic.17248$GR.2501445@attbi_s01...
> "Some Clown" <(E-Mail Removed)> 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[i].find(".m3u"))

>
> Change this to
>
> if (wholeCmdLine[i].find(".m3u") != std::string::npos)
>
> > {
> > std::string playListFileName = wholeCmdLine[i];
> > 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?


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-23-2004
>
> 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


 
Reply With Quote
 
Some Clown
Guest
Posts: n/a
 
      04-23-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:c6ad6n$9p4o3$(E-Mail Removed)-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.


 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      04-23-2004
In article <(E-Mail Removed)>,
Some Clown <(E-Mail Removed)> wrote:
>"John Harrison" <(E-Mail Removed)> wrote in message
>news:c6ad6n$9p4o3$(E-Mail Removed)-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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
Looping through dataset outputting HTML radiobuttons Robert H ASP .Net 0 12-04-2003 11:02 PM
Looping through rows in a DataGrid Diva ASP .Net 1 07-24-2003 06:10 PM
Looping through all the form fields in the code behind ? Aemca ASP .Net 1 07-22-2003 08:31 AM



Advertisments