473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Looping through a <vector>

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 ---------------------------
Jul 22 '05 #1
6 2491
"Some Clown" <no***@nowhere.net> 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
Jul 22 '05 #2
On Thu, 22 Apr 2004 20:29:04 -0700 in comp.lang.c++, "Some Clown"
<no***@nowhere.net> 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)
Jul 22 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5f0ic.17248$GR.2501445@attbi_s01...
"Some Clown" <no***@nowhere.net> 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?
Jul 22 '05 #4
>
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
Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-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.
Jul 22 '05 #6
In article <rr********************@comcast.com>,
Some Clown <no***@nowhere.net> wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on the elements of A and B, i.e. I want to do this:
1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
5
by: Ahmad | last post by:
Hi all, I have written a simple c++ app, as I'm still learning c++. The thing works flawlessly on VC++6, but just doesn't work on g++. The transliterate function which causes the problem is...
2
by: Russ Ford | last post by:
I'm trying to overload the << operator to output vectors, as follows: template <class T> ostream& operator << (ostream& o, const vector<T>& v) { o << "< "; copy (v.begin(), v.end(),...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public : .. int getNumber(){ return num_;}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.