【问题标题】:getting selected data from a vector then storing as a variable从向量中获取选定的数据,然后存储为变量
【发布时间】:2013-04-09 13:22:38
【问题描述】:

我目前将所有数据作为变量存储到向量中。我试图从该向量中获取选定的数据,然后将其存储为变量,以便我可以进行计算,将答案保存为变量,然后存储回我的原始向量中?

数据文件格式如下;

    a     b       c        d      e
    1    7.3     0.8      14    74.6     
    2    6.5     0.1      13     3.3     
    3   10.8     1.4      12    75.8     
    4   13.2     3.5       6    32.4

到目前为止,我的代码如下;

struct Weather
{
  int a_data;
  double b_data;
  double c_data;
  int d_data;
  double e_data;
  double ans_data;
};



int main () 
{
  using std::vector;
  using std::string;
  using std::getline;
  using std::cout;

  vector<Weather> data_weather;
  string line;
  ifstream myfile ("weatherdata.txt");

  if (myfile.is_open())
  {
    int count = 0;
    while (getline(myfile, line)) 
    {
      if (count > 6) 
      {
            int a, d; 
            double b, c, e;
            std::istringstream buffer(line); 
            std::string sun_as_string;
            if (buffer >> a >> b >> c >> d >>e_as_string) 
            {
                if (e_as_string == "---")
                {
                    e = 0.0;
                }
                else
                {
                    std::istringstream buffer2(e_as_string);
                    if (!(buffer2 >> e))
                    {
                        e = 0.0;
                    }
                }
                Weather objName = {a, b, c, d, e};
                data_weather.push_back(objName);  
            }
      }
      count++;
    }
    myfile.close();

    double temp_b, temp_c, temp_ans; //declaring my new variables
    for (auto it = data_weather.begin(); it != data_weather.end(); ++it)
    { 
        std::cout << it->b_data << " " << it->c_data << std::endl;

    }
 }

   }
   else 

   cout << "unable to open file";

   scat::pause("\nPress <ENTER> to end the program.");

   return 0;
}

任何帮助将不胜感激

【问题讨论】:

  • 如何将向量中的选定数据存储到变量中以供使用,然后放回向量中?
  • 例如,如果我想做 10.8 + 1.4 / 4,那么将答案存储在 temp_ans 中
  • 你能显示 Weather 对象的代码吗?这将有助于看到成员。
  • @Glenn 见上文,我编辑了问题
  • 谢谢。这很有帮助。

标签: c++ variables vector integer double


【解决方案1】:

我遗漏了一些明显的东西还是你只是需要这样做?

for (auto it = data_weather.begin(); it != data_weather.end(); ++it)
{ 
    it->ans_data = it->b_data * it->c_data;
}

解除对迭代器的引用为您提供对向量内现有对象的引用。您实际上并不需要临时变量。

更好的 C++11 替代方案是基于范围的 for 循环:

for (Weather& w : data_weather)
{
    w.ans_data = w.b_data * w.c_data;
}

给定您要使用的行索引列表,您可以执行以下操作:

Weather& w = data_weather[i]; // shortcut so you don't need to
                              // write data_waether[i] each time
w.ans_data = (w.b_data * w.c_data)/2;

其中 i 是您感兴趣的行的索引。您可能希望将其设为某种循环。我把它作为练习留给你:)

【讨论】:

  • 这难道不只是将所有 b_data 乘以 c_data,就像来自 b 和 c 的单个数据一样吗?还是我会像您展示的那样乘以,然后在需要时调用选定的答案?如果是这样,我会怎么做
  • 循环遍历vector内部的每个Weather对象,并将每个对象的b_data和c_data相乘,并存储在ans_data中,是的。我的印象是这就是你想要的,如果不是,请纠正我。
  • 是的,例如 10.8 * 1.4 /2 说我想将第 3 行和第 4 行的这个方程的答案相加,忽略 1&2 或任何组合,这很容易实现吗?
  • Weather&amp; w = data_weather[i]; w.ans_data = (w.b_data * w.c_data)/2; 其中i 是您想要的行的索引。对每一行执行此操作。
  • @ 谢谢,这可能适用于 c++ vs 2010 吗?如果可以的话,你能把这个理论应用到你的第一个答案吗?
【解决方案2】:

我的结构会有所不同。我将编写用于读取数据的代码作为operator&gt;&gt;Weather 结构:

std::istream &operator>>(std::istream &is, Weather &w) { 
    std::string line;
    std::getline(is, line);
    std::istringstream buffer(line); 

    buffer >> w.a >> w.b >> w.c >> w.d;
    if (!buffer >> w.e) // conversion will fail for "---" or not-present
        w.e = 0.0;
    return is;
}

这样可以简化读取数据:

std::ifstream myfile("Weather.txt");

std::string ign;
std::getline(myfile, ign);   // skip that pesky first line

// Now we can initialize the vector directly from the data in the file:
std::vector<Weather> weather_data((std::istream_iterator<Weather>(myfile)),
                                  std::istream_iterator<Weather>());

使用相当新的编译器,打印数据也可以简化:

for (auto &w : weather_data)
    std::cout << w.b_data << " " << w.c_data << "\n";

如果您想进行计算,并将结果放回结构的ans 字段中,这也相当容易。例如,假设bcd 是温度,我们希望ans 包含三者的平均值:

for (auto &w : weather_data)
     w.ans = (w.b_data + w.c_data + w.d_data)/3.0; 

【讨论】:

  • 谢谢,这是一种不同的方法,我今晚会看看。从那时起,将我的计算值从向量写入新的文本文件会相对容易吗?
  • @jaylad:是的:for (auto w : weather_data) some_file &lt;&lt; w.ans &lt;&lt; "\n";
  • 这会创建一个文本文件,还是我需要在写入数据之前这样做?
  • @jaylad:您将首先创建文本文件(使用std::ofstream("whatever.txt");),然后使用上述代码写入它。
  • 欣赏,以后会尝试这种方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2021-03-04
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多