【问题标题】:Create a vector of strings in one file and use it in another file在一个文件中创建一个字符串向量并在另一个文件中使用它
【发布时间】:2014-07-25 02:38:11
【问题描述】:

我有一长串字符串,我想在自己的 .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 向量的任何元素。如何将第一个头文件中的字符串值传递给第二个头文件?

【问题讨论】:

  • 谢谢!我仍然不确定我是否完全理解。我制作了一个 kitBreakdown.cpp 文件,它定义了向量并包含 kitBreakdown.h。 kitBreakdown.h 只有外部 std::vector<:string> Bricks_Plates;线。我运行时仍然得到一个空向量。
  • 尝试将代码减少到尽可能少的行,但仍然会产生问题。然后贴出代码。

标签: c++ string vector


【解决方案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 初始化为您从设置返回的值:

#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 的注释在技术上是正确的,并且使用外部变量或全局变量有时是唯一的方法,但使用全局变量被广泛认为是不好的风格。

【讨论】:

  • 非常感谢!如果除了 Bricks_Plates 之外还有更多向量,我可以为每个向量创建一个函数并以这种方式返回每个向量,对吗?
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 2017-11-30
  • 2019-03-21
  • 1970-01-01
  • 2014-02-13
  • 2013-09-20
  • 1970-01-01
  • 2011-05-22
相关资源
最近更新 更多