Looping through a <vector>

S

Some Clown

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 ---------------------------
 
V

Victor Bazarov

Some Clown said:
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
 
D

David Harmon

On Thu, 22 Apr 2004 20:29:04 -0700 in comp.lang.c++, "Some Clown"
------------------- 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)
 
S

Some Clown

Victor Bazarov said:
Some Clown said:
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?
 
J

John Harrison

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
 
S

Some Clown

John Harrison said:
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.
 
J

Jon Bell

[...] 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top