C++ 在记录集/更新值中搜索
我使用structs、vector创建了一个记录集,并添加了两条记录。这是执行此操作的代码。这应该在Arduino/ESP8266/ESP32上按原样运行C++ 在记录集/更新值中搜索,c++,vector,struct,esp32,C++,Vector,Struct,Esp32,我使用structs、vector创建了一个记录集,并添加了两条记录。这是执行此操作的代码。这应该在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
#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);
}
同样,我不确定这是否是最好的方法,但它是有效的。我希望能够更改studentSlot
,但我不确定,因为这是我第一次处理structs和vector。studentName是常量(我只需要在启动时加载一次),其中studentSlot可以在运行时更改。我不知道如何改变这一点。它可能需要我删除const char*,做一些strcpy之类的事情,但是我被困在那里了。简言之,有三件事我需要一些帮助
1) 按学生姓名搜索/查找记录
2) 能够更新studentSlot
3) 删除所有记录注意:我刚刚发现studentRecs.clear()
可以做到这一点
我不确定我是否能够足够清楚地解释这一点。所以任何问题都请回答。谢谢。好吧,我想你最好的选择是使用
for
循环来搜索学生名。取决于你使用的C++修订版:
student searchForName(const std::string & name)
{
for (auto item : studentRecs)
{
if (item.studentName == name)
return item;
}
return student();
}
或者,如果您受限于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();
}
student searchForName(const std::string&name)
{
对于(std::size_t cnt=0;cnt
其余的都很相似
顺便说一句:您可以更改:
...
// Get the size
int dsize = static_cast<int>(studentRecs.size());
// Loop, print the records
for (int i = 0; i < dsize; ++i) {
...
。。。
//知道尺寸了吗
int dsize=static_cast(studentRecs.size());
//循环,打印记录
对于(int i=0;i
致:
。。。
//循环,打印记录
对于(std::size_t i=0;i <>代码> 嗯,我认为最好的办法是与 < <代码>循环>搜索<代码>学生姓名< /> >。
student searchForName(const std::string & name)
{
for (auto item : studentRecs)
{
if (item.studentName == name)
return item;
}
return student();
}
或者,如果您受限于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();
}
student searchForName(const std::string&name)
{
对于(std::size_t cnt=0;cnt
其余的都很相似
顺便说一句:您可以更改:
...
// Get the size
int dsize = static_cast<int>(studentRecs.size());
// Loop, print the records
for (int i = 0; i < dsize; ++i) {
...
。。。
//知道尺寸了吗
int dsize=static_cast(studentRecs.size());
//循环,打印记录
对于(int i=0;i
致:
。。。
//循环,打印记录
对于(std::size_t i=0;i
感谢Diodacus简化了循环/大小部分。当前程序是一个.ino文件(Arduino IDE/C++)。因此,我不太确定有多少标准C/C++功能得到支持。因此,对于任何C/C++功能,我都必须进行反复试验。但到目前为止,能够使用字符串、向量库是一个很大的解脱。我不确定这些功能可以扩展到什么程度。感谢Diodacus简化了循环/大小部分。当前的程序是一个.ino文件(Arduino IDE/C++)。因此,我不太确定有多少标准C/C++功能得到支持。因此,对于任何C/C++功能,我都必须进行试错。但是,由于能够使用字符串,向量库到目前为止是一个很大的解脱。我不确定这些功能可以扩展到什么程度。