搜索记录集/更新值 [英] search in record set / update value

查看:124
本文介绍了搜索记录集/更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用结构,向量创建了一个记录集,并添加了几对记录。这是执行此操作的代码。

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);
}

我能够使用for循环读取单个记录。我不确定这是否是最好的方法,但确实可以。

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)搜索/查找 studentName 的记录。我可以通过循环找到它,但是这对我来说效率低下又难看。

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

2)能够更新 studentSlot wasPresent

通过一些研究和实验,我发现我可以这样做来更改 wasPresent

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

studentRecs[0].wasPresent = false;

同样,我不确定这是否是最好的方法,但它是否有效。我希望能够更改 studentSlot ,但我对此不确定,因为这是我第一次处理结构和向量。 studentName是恒定的(我只需要在启动时加载一次),即可在运行时更改studentSlot。我不确定该如何更改。可能需要我删除const char *,执行一些strcpy的操作或其他操作,但是我仍然坚持这样做。简而言之,有3件事我需要一点帮助

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)按学生姓名搜索/查找记录

1) Search/find a record by studentName

2)能够更新studentSlot

2) Be able to update studentSlot

3)删除所有记录。 注意:我刚刚发现 studentRecs.clear()会这样做

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

I我不确定我是否能够足够清楚地解释这一点。因此,如有任何疑问,请射击。谢谢。

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

推荐答案

好吧,我认为您最好的选择是循环以搜索 studentName 。取决于您使用的C ++版本:

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();
}

或者如果您限于C ++ 11之前的版本:

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();
}

其余的非常相似。

BTW .:您可以更改:

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) {
...

到:

...
  // Loop, print the records
  for (std::size_t i = 0; i < studentRecs.size(); ++i) {
...

这篇关于搜索记录集/更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆