Click here to Skip to main content
15,887,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: memory leak problem PinPopular
Rajesh R Subramanian6-Apr-11 22:02
professionalRajesh R Subramanian6-Apr-11 22:02 
Questionupdate structure in a vector of structures Pin
csrss6-Apr-11 8:53
csrss6-Apr-11 8:53 
AnswerRe: update structure in a vector of structures Pin
csrss6-Apr-11 9:19
csrss6-Apr-11 9:19 
AnswerRe: update structure in a vector of structures Pin
Niklas L6-Apr-11 20:55
Niklas L6-Apr-11 20:55 
GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 2:14
csrss7-Apr-11 2:14 
AnswerRe: update structure in a vector of structures Pin
Parthi_Appu6-Apr-11 23:32
Parthi_Appu6-Apr-11 23:32 
GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 2:18
csrss7-Apr-11 2:18 
AnswerRe: update structure in a vector of structures [modified] Pin
Stefan_Lang7-Apr-11 4:01
Stefan_Lang7-Apr-11 4:01 
As an aside, you don't need a typedef with every struct definition - you can use the 'tag' of the struct in the same way you can use a class name.

Two options:

1. If you really only have an int that serves as soem sort of unique ID, and some value associated to that ID, then you should use a map rather than a vector. std::map automatically sorts your elements by your key values when you insert them. Retrieving an item is a simple and very fast operation.

2. If there's more to it, you can still use the std::find() method to locate a specific struct in your vector. You only need to define an equality operator for your struct like this: (not tested)
#include <vector> // std::vector
#include <algorithm> //std::find()
struct S {
  int a;
  std::string b;
  bool operator==(const S& s) const {
    return s.a==a;
  }
  S(int a_) : a(a_) {} // constructor, just for convenience
  S(int a_, std::string b_) : a(a_), b(b_) {} // constructor, even more convenient
}

int main() {
  // ...
  std::vector<S> s;
  s.push_back(S(1, "hi"));
  // ...
  s.push_back(S(10, "hello"));

  std::vector<S>::iterator location = std::find(s.begin(), s.end(), 5); // finds element with a==5
  std::string result;
  if (location != s.end(); // found something?
    result = location->b;
}

You can check the online documentation on std::find() and std::find_if() for examples.

P.S.: anyone knows what is messing with code coloring here? Confused | :confused:

modified on Friday, April 8, 2011 2:40 AM

GeneralRe: update structure in a vector of structures Pin
csrss7-Apr-11 7:11
csrss7-Apr-11 7:11 
GeneralRe: update structure in a vector of structures Pin
Stefan_Lang7-Apr-11 20:44
Stefan_Lang7-Apr-11 20:44 
QuestionHow to create setup file of an EXE in Visual Studio 6.0 ? Pin
pjhelp6-Apr-11 4:10
pjhelp6-Apr-11 4:10 
AnswerRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Hans Dietrich6-Apr-11 4:13
mentorHans Dietrich6-Apr-11 4:13 
AnswerRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
DanYELL6-Apr-11 16:16
DanYELL6-Apr-11 16:16 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 17:05
professionalAlbert Holguin6-Apr-11 17:05 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Hans Dietrich6-Apr-11 17:33
mentorHans Dietrich6-Apr-11 17:33 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 17:42
professionalAlbert Holguin6-Apr-11 17:42 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Rajesh R Subramanian7-Apr-11 6:41
professionalRajesh R Subramanian7-Apr-11 6:41 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 17:44
professionalAlbert Holguin6-Apr-11 17:44 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Hans Dietrich6-Apr-11 18:04
mentorHans Dietrich6-Apr-11 18:04 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 18:07
professionalAlbert Holguin6-Apr-11 18:07 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 18:09
professionalAlbert Holguin6-Apr-11 18:09 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Hans Dietrich6-Apr-11 18:17
mentorHans Dietrich6-Apr-11 18:17 
GeneralRe: How to create setup file of an EXE in Visual Studio 6.0 ? Pin
Albert Holguin6-Apr-11 18:20
professionalAlbert Holguin6-Apr-11 18:20 
QuestionFormView on close Pin
Sakhalean6-Apr-11 3:23
Sakhalean6-Apr-11 3:23 
AnswerRe: FormView on close Pin
Hans Dietrich6-Apr-11 4:00
mentorHans Dietrich6-Apr-11 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.