# Assign and recall a Vector of Structs

Page 1 of 1

## 8 Replies - 4128 Views - Last Post: 10 December 2008 - 05:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=75473&amp;s=998984fc919456c33e7bdc63a92f5aa9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jchrispen

Reputation: 1
• 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

Reputation: 1
• 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

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• 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

### #4 _sc_

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

## Re: Assign and recall a Vector of Structs

Posted 07 December 2008 - 02:04 PM

Btw, string comparison:

jchrispen, 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) { ... }

```

### #5 jchrispen

Reputation: 1
• 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?

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• 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.

### #7 jchrispen

Reputation: 1
• 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?

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• 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

```

### #9 jchrispen

Reputation: 1
• 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.