Advertisement

Comparing vectors

Started by September 17, 2000 09:54 PM
2 comments, last by gimp 24 years, 3 months ago
I need to test the contents of two vectors against each other. I''ve written this but from the error I''m getting I wondering is this simple action is even possible:
    
	while(itr != Registry->end())
	{
		for (unsigned long i=0; i < itr->first.size(); i++)
		{
			if (Data<i> == itr->first[i])
			{
				matches=true;
			}
			else
				break;
		}
	}
    
and the error : error C2676: binary ''=='' : ''class std::vector >'' does not define this operator or a conversion to a type a cceptable to the predefined operator I can''t think of any other way of doing this... Basically I have two vectors, one of a fixed length stored in a map. The other will come in of a variable length but I only want to see if the beginning is the same. To put this in to context, the map contains the list of console commands, the variable length commands is what the user typed, I only need to test the command not the parameters... e.g. vector1 = "GAMMA" vector2 = "GAMMA 0.76" I only want to see if the beginning is the same... Many thanks for help you provide... gimp
Chris Brodie
That code does not make a bit of sense. Data < i > is a template instanciation(sp?) it cannot occur anywhere that int cannot occur. You never use ++itr, so the while loop is going to have a long wait. Plus you break out of the for loop the moment you miss, but don't exit when you find a match, which can only happen on the first one you are looking for. Plus why are these not strings?

Here is what I think you are trying to do.

        /* * We Have a heiarchy of commands each with a NAME  * and a virtual function GO(const string &param) * when the user types anything of the form * NAME [paramiters if applicapble] * go is called with the paramiters */class command {protected:  string name;  void set_name(const string &n) {    string prefix    name.reserve(n.size()+1);    transform(n.begin(),n.end(),name.begin(),tolower);      }  // if n has a lower case prefix we can use this  bool name_match(const string &n) const {     return name.compare(0,name.size(),n)==0;    }public:  command(const string &n) : name(0,'\0') {set_name(n);}  virtual ~command() {}  const string & get_name() const {return name;}  virtual bool go(const string &n) const {    cout << name << " Would perform " << n << endl;  }  bool operator < (const command &other) const {    return name < other.get_name();  }  bool operator == (const command &c) {    return name_match(c.get_name());  }};class commandset {  set<command> cmdspublic:  void AddCmd(const command &c) {    cmds.insert(c);  }  bool perform(const string &s) {    set<command>::const_iterator cmd=cmds.find(command(s));    if(cmd == cmds.end()) return false;    else return (*cmd).go(s);  }};          


Ok, thats a bit too fancy and generates a few temporaries but this should be easy enough. Just create derive class for each command and fill in go() with something apropreate. The method set_name() should never be called after the class is inserted in the set. The transform is just there to make commands case insenseative. Name is constructed empty to avoid wasting space,
hopefully only 1 new will be required on construction.

You may need to build a function class to pass to set, but the default should work fine, (thanks to the overloaded operators).


Edited by - Grib on September 18, 2000 1:46:56 AM
Advertisement
Thanks for the post... just as a matter of interest I think that the board ate some of my code my code only has square braces...No wonder it didn't make sence...

I never use itr++. My fault, I never even got the code to compile. General write a few lines of completed code, compile, write some more... etc

About rhe break, again your right... my coding got snagged on the comparison, something I new would can the whole method if I couldn't solve it, so no point in continuing to code something that might not work until I solve the problem.

I didn't use strings because this datatype should be able to hold strings -and- binary data(basically structs \ bitfields). My experience with strings are that as soon as you give it a null strange things happen, as you would expect, it's null delimited.

again thanks


Edited by - gimp on September 18, 2000 2:03:57 AM
Chris Brodie
Grib... thanks again but as I needed to work with the vector I can up with the following:

    #include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){  vector<char> v, v2;  vector<char>::iterator p;  char Data[] = "SCREEN 800 600";  char Command[] = "SCREEN";  int i;  for(i=0; i<strlen(Data); i++)    v.push_back(Data<i>);  for(i=0; i<strlen(Command); i++)    v2.push_back(Command[i]);  p = search(v.begin(), v.begin()+v2.size(), v2.begin(), v2.end());  if (p != v.begin()+v2.size())	  cout << "Command found" << endl;  else	  cout << "Command not found" << endl;  return 0;}    


(Hope the board didn''t eat that code too...)

gimp
Chris Brodie

This topic is closed to new replies.

Advertisement