Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-24-2003, 02:52 PM   #1 (permalink)
sofa
Registered User
 
Join Date: Mar 2003
Posts: 1
sofa is on a distinguished road
comparing words to those in a vector?

hey everyone, this is my first post, and I have been learning c++ for a few months now (and boy, it's confusing!)

Right now, I"m trying to select words. If they are in a vector of words that are 'taboo' they are rejected. if not, they are placed into a map.

So far, I have been looping through the vector as i compare to each element, but for some reason, this is screwing up the count. Is comparing to each element the only way to go?
instead of counting each 'good' word, it prints out the map, but every word with an associative number incrementing by 4 is displayed.
sofa is offline   Reply With Quote
Old 03-24-2003, 06:47 PM   #2 (permalink)
Danish
Registered User
 
Join Date: Mar 2003
Posts: 12
Danish is on a distinguished road
Send a message via AIM to Danish
I don't understand. You're comparing words (strings?) to words in a vector, then moving them to a map(?) once you're done. That sounds like a logic error. Could we see the problematic code?
Danish is offline   Reply With Quote
Old 03-24-2003, 07:04 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
snippets

Declaring a string vector is simple:
Code:
vector <string>vecStringTabooWords;
The size of a vector is determined by: vecStringTabooWords.size();

So iterating a vector goes like:
Code:
for(int i = 0; i < vecStringTabooWords.size(); i++)
Comparing goes like: vecStringTabooWords.[i] ==some_string

Well, I don't know what else to type. Just post the code and the error messages if any.
Valmont is offline   Reply With Quote
Old 03-31-2003, 10:57 AM   #4 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
To look for something in a container, you can use the 'find' function from the <algorithm> header. To look for a word in your vector, you can use it like this:

Code:
vector<string>::iterator found (std::find (taboo.begin(), taboo.end(), some_word));
'found' will now point to the word you were looking for, or to 'taboo.end()' if it wasn't found.

If all the words in your taboo list are unique, consider using a set instead. A set will sort its elements for you, and you can be sure you are not wasting precious memory by storing the same word twice. Also, because the container is sorted, looking for a certain word is a lot faster.

Code:
set<string> taboo_set;
taboo_set.insert ("foo");

if (taboo_set.count ("bar") == 0)
  // The word "bar" is not in the map
Unicorn is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Vector Belisarius Java 5 08-07-2003 07:53 AM
vector of vectors zergmuncher Standard C, C++ 5 05-15-2003 08:15 AM


All times are GMT -8. The time now is 05:06 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting