在一个文件中创建字符串向量,并在另一个文件中使用它

Create a vector of strings in one file and use it in another file

本文关键字:文件 另一个 向量 创建 一个 字符串      更新时间:2023-10-16

我有一长串字符串,我想把定义和声明在自己的。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的注释在技术上是正确的,使用外部或全局变量有时是做事情的唯一方法,但使用全局变量被广泛认为是糟糕的风格。

相关文章: