search in record set / update value

2019-06-11 12:36发布

I created a record set using structs, vector and added couple of records. This is the code that does it. This should run as is - on an Arduino/ESP8266/ESP32.

#include <string>
#include <vector>

struct student {

  std::string studentName; // I only load this once at startup. So can be const
  std::string studentSlot; // <= This should be updateable
  bool wasPresent;         // <= This should be updateable

  student(const char* stName, const char* stSlot, bool stPresent) :
    studentName(stName),
    studentSlot(stSlot),
    wasPresent(stPresent)
  {}
};

std::vector<student> studentRecs;

void setup() {
  delay(1000);

  Serial.begin(115200);

  // Add couple of records
  student record1("K.Reeves", "SLT-AM-03", false);
  student record2("J.Wick", "SLT-PM-01", true);

  studentRecs.push_back(record1);
  studentRecs.push_back(record2);
}

void loop() {

  Serial.println();

  // Get the size
  int dsize = static_cast<int>(studentRecs.size());

  // Loop, print the records
  for (int i = 0; i < dsize; ++i) {
    Serial.print(studentRecs[i].studentName.c_str());
    Serial.print(" ");
    Serial.print(studentRecs[i].studentSlot.c_str());
    Serial.print(" ");
    Serial.println(String(studentRecs[i].wasPresent));
  }

  // Add a delay, continue with the loop()
  delay(5000);
}

I am able to read the individual records using the for loop. I am not sure if it's the best way but it does work.

I need to be able to do couple of things on this record set.

1) Search/find a record by studentName. I can find it by looping but that comes across as inefficient+ugly to me.

2) Be able to update studentSlot and wasPresent

With some research and experimentation I found that I could do this to change wasPresent

studentRecs[0].wasPresent = false;

Again I am not sure if that is the best way but it works. I want to be able to change studentSlot and I am not sure about that as this is my first time dealing with structs and vector. The studentName is constant ( I only need to load it once at startup ) where the studentSlot can change during runtime. I am not sure how to change that. It may need me to remove the const char*, do some strcpy thingy or something but I am stuck at that. In short there are 3 things I need a little help on

1) Search/find a record by studentName

2) Be able to update studentSlot

3) Delete all the records. Note: I just figured out that studentRecs.clear() does this

I am not sure if I have been able to explain this clearly enough. So any questions please shoot. Thanks.

1条回答
\"骚年 ilove
2楼-- · 2019-06-11 13:16

Well, I think your best bet is to go with for loop to search for studentName. Depending on what C++ revision you are using:

student searchForName(const std::string & name)
{
    for (auto item : studentRecs)
    {
        if (item.studentName == name)
            return item;
    }
    return student();
}

or if you are constrained to pre-C++11:

student searchForName(const std::string & name)
{
    for (std::size_t cnt = 0; cnt < studentRecs.size(); ++cnt)
    {
        if (studentRecs[cnt].studentName == name)
            return item;
    }
    return student();
}

The rest is very similar.

BTW.: You can change:

...
  // Get the size
  int dsize = static_cast<int>(studentRecs.size());

  // Loop, print the records
  for (int i = 0; i < dsize; ++i) {
...

to:

...
  // Loop, print the records
  for (std::size_t i = 0; i < studentRecs.size(); ++i) {
...
查看更多
登录 后发表回答