8 Replies - 3455 Views - Last Post: 10 December 2008 - 05:00 PM Rate Topic: -----

#1 jchrispen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Assign and recall a Vector of Structs

Posted 07 December 2008 - 10:39 AM

I am trying to pull information back out of my vector and assign values to my vector. The vector is of the datatype struct Node (see below) and my function is displayed below.

I am getting a compile error: no match for call to `(std::vector<Node, std::allocator<Node> >) (int&)'

has anyone seen this?

struct Node{
	   bool inShortPath;
	   double cost, totalCost;
	   std::string srcNode, dstNode;
};

std::string bestPath(std::string& point, std::vector<Node>& set, double & total){
	int nodeNum = -1;
	double lowest = std::numeric_limits< double >::infinity();
	
	for(int i = 0; i < set.size(); i++){
	   if ( strcmp( point.c_str(), set(i).srcNode.c_str() ) == 0 ){
		  set(i).totalCost = set(i).cost + total;
		  if (lowest > set(i).cost){
			 lowest = set(i).cost;
			 nodeNum = i;  
		  }
	   }
	}		
	return set(nodeNum).dstNode;
}



-jason

Is This A Good Question/Topic? 0
  • +

Replies To: Assign and recall a Vector of Structs

#2 jchrispen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Assign and recall a Vector of Structs

Posted 07 December 2008 - 11:14 AM

Oh wait I think it the code to access an element of looks like the following doesn't it.

std::vector<Node> v;
std::string node;

node = v.at(index).srcNode;



-jason
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Assign and recall a Vector of Structs

Posted 07 December 2008 - 11:24 AM

Yup. Access the element index, then call its members/functions, etc...

All you could possibly want to know about vectors
Was This Post Helpful? 0
  • +
  • -

#4 _sc_  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 2
  • Joined: 07-December 08

Re: Assign and recall a Vector of Structs

Posted 07 December 2008 - 02:04 PM

Btw, string comparison:

View Postjchrispen, on 7 Dec, 2008 - 09:39 AM, said:

if ( strcmp( point.c_str(), set(i).srcNode.c_str() ) == 0 ){


looks wild. I'd better suggest the following:

if (point == set[i].srcNode) { ... }


Was This Post Helpful? 0
  • +
  • -

#5 jchrispen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Assign and recall a Vector of Structs

Posted 10 December 2008 - 04:30 PM

It was my understanding that the Boolean comparison == could produce errors if used on strings. Is that not true?
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Assign and recall a Vector of Structs

Posted 10 December 2008 - 04:31 PM

Generally it should be ok. In an ideal world you would write a comparison function similar to java's equals() to compare strings.
Was This Post Helpful? 0
  • +
  • -

#7 jchrispen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Assign and recall a Vector of Structs

Posted 10 December 2008 - 04:45 PM

Thanks for the insight.

I'm familar with the Java version. It compares the two string objects. C++ doesn't have that built-in, hence your comment to write one. The strcmp will compare C style strings. Is there a "best practice" when comparing strings in C++?

When you say generally it should be ok, when is it not?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Assign and recall a Vector of Structs

Posted 10 December 2008 - 04:54 PM

If you're getting input from cin, or other stream and the input isn't checked cin.sync(); cin.ignore()//etc... you may or may not know for sure what you're getting and that could throw off a regular == comparison. You can compare C++ strings with strcmp, just use:

string str = "whatever";
string strTwo = "KYAisAwesome!";
strcmp(str.c_str(), strTwo.c_str()); //strName.c_str() makes it the equivalent of a c style string


Was This Post Helpful? 1
  • +
  • -

#9 jchrispen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Assign and recall a Vector of Structs

Posted 10 December 2008 - 05:00 PM

Ok and that makes sense. Thanks for the information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1