在一个文件中创建字符串向量,并在另一个文件中使用它
Create a vector of strings in one file and use it in another file
我有一长串字符串,我想把定义和声明在自己的。h文件。我想将这些字符串分组为向量,并在另一个。h文件中使用这些值。第二个文件将std::find查看一个字符串是否在vector中。向量是一个很好的方法组字符串做到这一点,还是我应该使用另一种方法?
我有一个kitbreak .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 anothern";
i=1;
}
else {
std::cout << "I don't recognize what you wantn";
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向量的任何元素。我如何将第一个头文件中的字符串值传递给第二个头文件?
修改您的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 anothern";
i=1;
}
else {
std::cout << "I don't recognize what you wantn";
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的注释在技术上是正确的,使用外部或全局变量有时是做事情的唯一方法,但使用全局变量被广泛认为是糟糕的风格。
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- C++ 如何打开带有另一个目录的输入文件?
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 如何在另一个 CPP 文件中使用一个 cpp 文件中的函数?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 使用命名空间在另一个".cpp"文件中定义
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?