我有很长的字符串列表,我想在其自己的.h文件中定义和声明。我想将这些字符串分组为向量,并在不同的.h文件中使用这些值。第二个文件将std :: find查看字符串是否在向量中。向量是一种很好的方法,可以将字符串分组,还是应该使用其他方法?
我有一个kitBreakdown.h文件将有多个字符串向量,如下所示:
#ifndef KIT_BREAKDOWN_H
#define KIT_BREAKDOWN_H
#include <vector>
void setup(){
std::vector<std::string> Bricks_Plates;
Bricks_Plates.push_back("2_1_plate"); //4211398
Bricks_Plates.push_back("2_1_brick"); //4211388
Bricks_Plates.push_back("2_2_brick"); //4211387
Bricks_Plates.push_back("4_1_plate"); //4211445
Bricks_Plates.push_back("4_2_plate"); //4211444
Bricks_Plates.push_back("6_2_plate"); //4211542
Bricks_Plates.push_back("8_2_plate"); //4211449
Bricks_Plates.push_back("2_1_smooth_plate"); //4211052
}
#endif
我想在另一个名为searchControl.h的文件中使用这些字符串,该文件包含一个searchControl类来实现机器人搜索。
#include "kitBreakdown.h"
#include <algorithm>
// The purpose of this class is to implement a search control structure
// So individual variables can be set up (limbs and cameras) before hand
// Search Geometry should be set and checked to ensure valid choices are made
class SearchControl
{ ...
private:
void _init_search();
...
std::vector<std::string> Bricks_Plates;
};
void SearchControl::_init_search()
{...
std::cout<<"What is your Desired Piece Type?\n";
int i = 0;
while (i==0)
{
std::cin >> _desired_piece;
if (std::find(Bricks_Plates.begin(),Bricks_Plates.end(), _desired_piece) !=Bricks_Plates.end())
{
std::cout << "Cool. " << _desired_piece << " will go in one bin and anything else will go in another\n";
i=1;
}
else {
std::cout << "I don't recognize what you want\n";
std::cout << "Your Choices are...\n";
for (int j=0; j<Bricks_Plates.size(); j++) {
std::cout<< Bricks_Plates[j]<< "\n";
}
std::cout << "Enter a new Desired Piece Type:\n";
}
}
}
我希望这个要求_desired_piece,检查_desired_piece是否在Brick_Plates向量中,并相应地执行if语句。但是,当我运行此代码时,它不输出Brick_Plates向量的元素。如何将第一个头文件中的字符串值传递给第二个?
答案 0 :(得分:1)
修改你的steup函数以返回你构建的向量:
#ifndef KIT_BREAKDOWN_H
#define KIT_BREAKDOWN_H
#include <vector>
std::vector<std::string> setup(){
std::vector<std::string> Bricks_Plates;
Bricks_Plates.push_back("2_1_plate"); //4211398
Bricks_Plates.push_back("2_1_brick"); //4211388
Bricks_Plates.push_back("2_2_brick"); //4211387
Bricks_Plates.push_back("4_1_plate"); //4211445
Bricks_Plates.push_back("4_2_plate"); //4211444
Bricks_Plates.push_back("6_2_plate"); //4211542
Bricks_Plates.push_back("8_2_plate"); //4211449
Bricks_Plates.push_back("2_1_smooth_plate"); //4211052
return Bricks_Plates;
}
#endif
并向SearchControl
添加一个构造函数,将其成员Bricks_Plates
初始化为您从setup中返回的值:
#include "kitBreakdown.h"
#include <algorithm>
class SearchControl
{ ...
public:
SearchControl():Bricks_Plates(setup()){}
private:
void _init_search();
...
std::vector<std::string> Bricks_Plates;
};
void SearchControl::_init_search()
{...
std::cout<<"What is your Desired Piece Type?\n";
int i = 0;
while (i==0)
{
std::cin >> _desired_piece;
if (std::find(Bricks_Plates.begin(),Bricks_Plates.end(), _desired_piece) !=Bricks_Plates.end())
{
std::cout << "Cool. " << _desired_piece << " will go in one bin and anything else will go in another\n";
i=1;
}
else {
std::cout << "I don't recognize what you want\n";
std::cout << "Your Choices are...\n";
for (int j=0; j<Bricks_Plates.size(); j++) {
std::cout<< Bricks_Plates[j]<< "\n";
}
std::cout << "Enter a new Desired Piece Type:\n";
}
}
}
虽然R Sahus评论在技术上是正确的,并且使用extern或全局变量有时是做事的唯一方法,但它被广泛认为是使用全局变量的坏风格。