LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2011, 03:09 AM   #1
robertodb
LQ Newbie
 
Registered: Sep 2011
Posts: 13

Rep: Reputation: Disabled
Unhappy [C++] comparison vector and array of char


I have to write an application in which a text file is read and stored in a vector of strings
Code:
vector<string> text;
After that I have to compare each character of text loaded from a file with an array of char with default content
Code:
char ARRAY[21] = {... ... ...}
so I thought something like that
Code:
for(int i = 0; i < text.size(); i++){
               for(int j = 0; j < 21; j++){
                       if(text[i] == ARRAY[j])
                       index_t[i] = j;
               }        
       }
at compile time I get the following error:
no match for 'operator==' in '(((std::vector<std::string, std::allocator<std::string> >*)
how can I fix that?
 
Old 09-20-2011, 03:30 AM   #2
Sylvester Ink
Member
 
Registered: Jun 2010
Distribution: Slackware
Posts: 112

Rep: Reputation: 35
It seems like currently you are iterating through the vector of strings. For each string you are comparing it to each char element in the array, and if it matches keep track of which char matched in that particular string. However, currently you are comparing a string and a char, and since these elements aren't the same type, they can't be compared. Instead you would need to add another for loop to iterate through each character of the string and compare each character with the char. You could also use the find() method of the string if you just need to check if the string contains that char.
 
1 members found this post helpful.
Old 09-20-2011, 05:53 AM   #3
robertodb
LQ Newbie
 
Registered: Sep 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
I will be more clear: I have to compare each character of each string in vector. If the comparison is positive, I associate the array index corresponding to the letter to a variable. For example, if the character of a string in vector is 'R', the index variable assumes a value of 15, as ARRAY [15] = R.
 
Old 09-20-2011, 06:06 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by robertodb View Post
I will be more clear: I have to compare each character of each string in vector. If the comparison is positive, I associate the array index corresponding to the letter to a variable. For example, if the character of a string in vector is 'R', the index variable assumes a value of 15, as ARRAY [15] = R.
I think you were pretty clear the first time. So was Sylvester Ink's response, although I think he meant to indicate that you should use find_first_of(), and not find().

Code:
std::vector<std::string> text;
size_t* index_t = new size_t[text.size()];

for (size_t i = 0; i < text.size(); ++i)
{
   size_t pos = text[i].find_first_of(ARRAY);

   if (pos != std::string::npos)
   {
      index_t[i] = pos;
   }
}

...

delete [] index_t;
Refer to these links for more information on STL string:

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/s...find_first_of/

Last edited by dwhitney67; 09-20-2011 at 06:09 AM.
 
2 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
How to convert short array to char array? bvkim Programming 4 06-08-2010 09:26 AM
C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" heathf Programming 2 04-25-2009 09:20 PM
Convert C++ String Vector to char array anamericanjoe Programming 1 12-12-2006 09:29 PM
vector copying speed comparison gecoool Programming 1 11-15-2005 10:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration