C ++编程任务
所以我有一个字符串列表
static const string mCodes[] = {
"AAD - Architecture Design",
"AAE - Architecture",
"AAI - Interior Design",
"AAL - Landscape Architecture",
"AAP - Urban Planning",
"AAS - Afro-American Studies",
"ABS - Architecture Building Science",
"ACA - Architecture Construction Mgmt",
"ACC - Accounting",
"AES - Aerospace Studies"};
我让用户使用学生的专业代码(例如,studentMajor = ACC)将studentMajor定义为2-4个字符串
我需要搜索字符串列表,如果它在该字符串列表中找到ACC,则会将studentMajor设置为在其中找到ACC的完整字符串。
if( student variable is found ) then
studentVariable = "ACC - Accounting"
else
error, improper student major entered
我假设我需要使用std :: find,但是我不确定如何在该列表中查找“ ACC”,然后将完整的字符串“ ACC”附加并存储到变量中
我认为对std :: string :: find和std :: string :: substr的操作很简单,但是我不确定如何解决这个问题。
谢谢
答案 0 :(得分:2)
一种方法是使用std::for_each
并仅在每个完整字符串中搜索提供的字符串。我还将使用std::vector
中的std::string
来存储字符串。示例:
#include <iostream>
#include <algorithm>
#include <vector>
const std::vector<std::string> mCodes = {
"AAD - Architecture Design",
"AAE - Architecture",
"AAI - Interior Design",
"AAL - Landscape Architecture",
"AAP - Urban Planning",
"AAS - Afro-American Studies",
"ABS - Architecture Building Science",
"ACA - Architecture Construction Mgmt",
"ACC - Accounting",
"AES - Aerospace Studies"};
std::vector<std::string> find_matches(const std::string& in) {
std::vector<std::string> rv;
// loop through all mCodes
std::for_each(mCodes.begin(), mCodes.end(),
// call lambda function for each mCode
[&](const std::string& full) {
// check if the "in" string can be found in "full"
if(full.find(in)!=std::string::npos)
// if so, save "full" to "rv"
rv.push_back(full);;
}
);
// return a vector of all found strings
return rv;
}
如果要使用2-4个字母代码的精确匹配,则可以使用std::unordered_map
,在代码和课程全名之间进行映射。示例:
#include <iostream>
#include <vector>
#include <unordered_map>
const std::unordered_map<std::string, std::string> mCodes = {
{"AAD", "Architecture Design"},
{"AAE", "Architecture"},
{"AAI", "Interior Design"},
{"AAL", "Landscape Architecture"},
{"AAP", "Urban Planning"},
{"AAS", "Afro-American Studies"},
{"ABS", "Architecture Building Science"},
{"ACA", "Architecture Construction Mgmt"},
{"ACC", "Accounting"},
{"AES", "Aerospace Studies"}
};
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
for(auto& str : args) {
auto it = mCodes.find(str);
if(it != mCodes.end()) {
std::cout << it->first << " - " << it->second << "\n";
}
}
}
答案 1 :(得分:0)
我通过在for循环中运行它解决了这个问题。
for(int i = 0; i < 221; i++){
if(mCodes[i].find(testMajor) == string::npos){
testMajor = mCodes[i];
return testMajor;
}
}
其中221是数组mCode的完整大小。这只是手动搜索mCodes的每个元素,如果搜索返回正数,则将testMajor设置为数组的该元素:)
谢谢你们
答案 2 :(得分:0)
您的问题将使自己适应地图,该地图将课程代码用作地图的关键。这是一个简单的例子。
#include <iostream>
#include <string>
#include <map>
static const std::string mCodes[] = {
"AAD - Architecture Design",
"AAE - Architecture",
"AAI - Interior Design",
"AAL - Landscape Architecture",
"AAP - Urban Planning",
"AAS - Afro-American Studies",
"ABS - Architecture Building Science",
"ACA - Architecture Construction Mgmt",
"ACC - Accounting",
"AES - Aerospace Studies"};
int main()
{
std::map<std::string, std::string> course_map;
for(auto const& course: mCodes) {
std::string code = course.substr(0, course.find(" "));
course_map.insert(std::pair<std::string, std::string>(code, course));
}
if(course_map.find("ACC") != course_map.end() ) {
std::cout << "course is " << course_map["ACC"] << std::endl;
} else {
std::cout << "ACC not found." << std::endl;
}
return 0;
}