如何将我的列声明为向量?

Ric*_*lev -2 c++ vector

我的代码

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <sstream>
#include <cmath>

int main(){
    std::ifstream ifs("MFSO7.dat");
    std::string line;

    while(std::getline(ifs, line)) // read one line from ifs
    {
        std::istringstream iss(line); // access line as a stream
        float column1;
        float column2;
        float column3;

        iss >> column1;
        std::cout << column1 << std::endl;
    }

   std::vector<float> v1 = column1;
   std::vector<float> v2;
   transform(v1.begin(), v1.end(), back_inserter(v2),[](float n){return std::pow(n,2);});
   copy(v2.begin(), v2.end(), std::ostream_iterator<float>( std::cout, " "));

}
Run Code Online (Sandbox Code Playgroud)

我从我的txt文件中读了三列,然后我只需要第一个用于进一步计算.我想用pow来对所有元素进行平方.但是我得到了

k.cpp: In function ‘int main()’:
k.cpp:24:28: error: ‘column1’ was not declared in this scope
    std::vector<float> v1 = column1;
Run Code Online (Sandbox Code Playgroud)

怎么解决这个?

atk*_*ins 5

将元素添加到循环内的向量中:

std::vector<float> v1;
while(std::getline(ifs, line)) // read one line from ifs
{
    std::istringstream iss(line); // access line as a stream
    float column1;

    iss >> column1;
    v1.push_back(column1);
    std::cout << column1 << std::endl;
}

// now v1 contains the first column of numbers from your file
// go ahead and transform it into v2
Run Code Online (Sandbox Code Playgroud)