C++ c++;将文本文件读入矢量<;向量>;然后根据内部向量中的第一个字写入向量或数组
这是我贴出的问题的进展 . 我现在进入了真正的应用程序,我在其中读取了如下文件:C++ c++;将文本文件读入矢量<;向量>;然后根据内部向量中的第一个字写入向量或数组,c++,vector,switch-statement,2d,C++,Vector,Switch Statement,2d,这是我贴出的问题的进展 . 我现在进入了真正的应用程序,我在其中读取了如下文件: MESH2D MESHNAME "default coverage" NUM_MATERIALS_PER_ELEM 1 E4Q 1 19 20 14 16 2 E4Q 2 17 16 15 23 2 E4Q 3 22 15 14 21 2 E4Q 4 4 3 21 20 1 E4Q 5 6 20 19 7 1 E4Q 6 18 17 10 9 1 E4Q 7 17 23 12 11 1 E4Q 8 7 19 18
MESH2D
MESHNAME "default coverage"
NUM_MATERIALS_PER_ELEM 1
E4Q 1 19 20 14 16 2
E4Q 2 17 16 15 23 2
E4Q 3 22 15 14 21 2
E4Q 4 4 3 21 20 1
E4Q 5 6 20 19 7 1
E4Q 6 18 17 10 9 1
E4Q 7 17 23 12 11 1
E4Q 8 7 19 18 8 1
E4Q 9 22 1 13 23 1
E3T 10 14 20 21 2
E3T 11 21 2 22 1
E3T 12 21 3 2 1
E3T 13 22 2 1 1
E3T 14 5 20 6 1
E3T 15 20 5 4 1
E3T 16 16 14 15 2
E3T 17 23 13 12 1
E3T 18 22 23 15 2
E3T 19 17 11 10 1
E3T 20 17 18 16 2
E3T 21 8 18 9 1
E3T 22 18 19 16 2
ND 1 -3.25811078e+002 7.70285567e+001 0.00000000e+000
ND 2 -3.24209146e+002 7.60394871e+001 0.00000000e+000
ND 3 -3.23012110e+002 7.44783503e+001 0.00000000e+000
ND 4 -3.22754089e+002 7.25326647e+001 0.00000000e+000
ND 5 -3.23617358e+002 7.08079432e+001 0.00000000e+000
ND 6 -3.25161538e+002 6.98134116e+001 0.00000000e+000
ND 7 -3.27128620e+002 6.98759747e+001 0.00000000e+000
ND 8 -3.29095703e+002 6.99385378e+001 0.00000000e+000
ND 9 -3.30301095e+002 7.14667646e+001 0.00000000e+000
ND 10 -3.30786908e+002 7.33241555e+001 0.00000000e+000
ND 11 -3.30835733e+002 7.52916270e+001 0.00000000e+000
ND 12 -3.29587322e+002 7.65401204e+001 0.00000000e+000
ND 13 -3.27743000e+002 7.72270000e+001 0.00000000e+000
ND 14 -3.26108525e+002 7.32067724e+001 0.00000000e+000
ND 15 -3.27041416e+002 7.42070316e+001 0.00000000e+000
ND 16 -3.27350377e+002 7.31716751e+001 0.00000000e+000
ND 17 -3.29153676e+002 7.40024406e+001 0.00000000e+000
ND 18 -3.28659180e+002 7.19967464e+001 0.00000000e+000
ND 19 -3.26845856e+002 7.14062637e+001 0.00000000e+000
ND 20 -3.25000347e+002 7.20534611e+001 0.00000000e+000
ND 21 -3.24701329e+002 7.39638966e+001 0.00000000e+000
ND 22 -3.26167714e+002 7.53360591e+001 0.00000000e+000
ND 23 -3.28060316e+002 7.54194849e+001 0.00000000e+000
BEGPARAMDEF
GM "Mesh"
SI 0
DY 0
TU ""
TD 0 0
NUME 3
BCPGC 0
DISP_OPTS entity 0 0 0 0 1 0 0 0
DISP_OPTS inactive 0 0 0 0 1 0 0 0
DISP_OPTS multiple 0 0 0 0 1 0 0 0
BEFONT 0 1
DISP_OPTS entity 1 0 0 0 1 0 0 0
DISP_OPTS inactive 1 0 0 0 1 0 1 0
DISP_OPTS multiple 1 0 0 0 1 0 1 0
BEFONT 1 1
DISP_OPTS entity 2 0 0 0 1 0 0 0
DISP_OPTS inactive 2 0 0 0 1 0 1 0
DISP_OPTS multiple 2 0 0 0 1 0 1 0
BEFONT 2 1
MAT 1 "material 01"
MAT 2 "material 02"
MAT_MULTI 0
ENDPARAMDEF
BEG2DMBC
END2DMBC
BEGCURVE Version: 1
ENDCURVE
调用'example.2dm'并尝试为存储在从E4Q E3T和ND开始的行中的数据写入3个2d向量(或可能(最好)数组)。我想使用这些单元格定义打印带有单元格中心的文件
我认为在这里记录这类问题是非常有用的,因为我的最后一个问题一直在获得意见
到目前为止,我的代码是:
// Read in CSV
//
// Alex Byasse
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
enum case_name {e3t, e4q, nd};
case_name checkit(std::string const& inString)
{
if (inString == "E3T") return e3t;
if (inString == "E4Q") return e4q;
if (inString == "ND") return nd;
}
int main()
{
std::vector<std::vector<std::string>> values;
std::ifstream fin("example.2dm");
for (std::string line; std::getline(fin, line); )
{
std::istringstream in(line);
values.push_back(
std::vector<std::string>(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>()));
}
int nc = 0;
int nn = 0;
std::vector<std::vector<double>> cells;
std::vector<std::vector<double>> nodes;
for (int i = 0; i < values.size() - 1; i++) {
switch (checkit(values[i][0])){
case e3t:
cells[nc].push_back(std::stod(values[i][1]));
cells[nc].push_back(std::stod(values[i][2]));
cells[nc].push_back(std::stod(values[i][3]));
cells[nc].push_back(std::stod(values[i][4]));
cells[nc].push_back(std::stod(values[i][2]));
cells[nc].push_back(std::stod(values[i][5]));
nc++;
break;
case e4q:
cells[nc].push_back(std::stod(values[i][1]));
cells[nc].push_back(std::stod(values[i][2]));
cells[nc].push_back(std::stod(values[i][3]));
cells[nc].push_back(std::stod(values[i][4]));
cells[nc].push_back(std::stod(values[i][5]));
cells[nc].push_back(std::stod(values[i][6]));
nc++;
break;
case nd:
nodes[nn].push_back(std::stod(values[i][1]));
nodes[nn].push_back(std::stod(values[i][2]));
nodes[nn].push_back(std::stod(values[i][3]));
nn++;
break;
}
}
}
我正在cygwin上运行g++。我已经检查了手册页,并且-std=c++11是一个可用选项,我已经尝试过了,但是我得到了相同的错误?您的程序产生了很多编译错误。我建议你多读一些C++和STL的基础知识。
std::向量值代码>到标准::向量值代码>。注意差异,一些C++编译器将采用<代码> >代码>作为右移运算符。
vector
而不是std::vector
。纠正这个错误,或者考虑使用命名空间STD添加<代码>;<代码>在主函数之前double
是一种基本类型。没有所谓的std::double
vector::size(values)-1更改为values.size()-1
而不是,
来分隔语句的三个部分
切换值[i][0]
它应该是切换(值[i][0])
E3T
是一个字符串,因此您可以使用“E3T”
而不是“E3T”
。单引号用于字符您的程序会产生很多编译错误。我建议你多读一些C++和STL的基础知识。
std::向量值代码>到标准::向量值代码>。注意差异,一些C++编译器将采用<代码> >代码>作为右移运算符。
vector
而不是std::vector
。纠正这个错误,或者考虑使用命名空间STD添加<代码>;<代码>在主函数之前double
是一种基本类型。没有所谓的std::double
vector::size(values)-1更改为values.size()-1
而不是,
来分隔语句的三个部分
切换值[i][0]
它应该是切换(值[i][0])
E3T
是一个字符串,因此您可以使用“E3T”
而不是“E3T”
。单引号用于字符有
单元[nc]
和节点[nd]
的位置被破坏,因为这些向量的大小从未增加
这里有一些东西可以满足您的需要,而不需要std::stod
。我将其更改为使用ND行中的所有值(假设这是您的意思),如果不是,则将GetValues调用中的4更改为3
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
double ToDouble(const std::string& str)
{
std::stringstream s(str);
double d;
s >> d;
return d;
}
std::vector<double> GetValues(const std::vector<std::string>& src, int start, int end)
{
std::vector<double> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(ToDouble(src[i]));
}
return ret;
}
void PrintValues(const std::string& title, std::vector<std::vector<double>>& v)
{
std::cout << title << std::endl;
for(size_t line = 0; line < v.size(); ++line)
{
for(size_t val = 0; val < v[line].size(); ++val)
{
std::cout << v[line][val] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::vector<std::vector<std::string>> values;
std::ifstream fin("example.2dm");
for (std::string line; std::getline(fin, line); )
{
std::istringstream in(line);
values.push_back(
std::vector<std::string>(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>()));
}
std::vector<std::vector<double>> cells;
std::vector<std::vector<double>> nodes;
for (size_t i = 0; i < values.size(); ++i)
{
if(values[i][0] == "E3T")
{
cells.push_back(GetValues(values[i], 1, 5));
}
else if(values[i][0] == "E4Q")
{
cells.push_back(GetValues(values[i], 1, 6));
}
else if(values[i][0] == "ND")
{
nodes.push_back(GetValues(values[i], 1, 4));
}
}
PrintValues("Cells", cells);
PrintValues("Nodes", nodes);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
双ToDouble(const std::string和str)
{
std::strings(str);
双d;
s>>d;
返回d;
}
std::vector GetValues(const std::vector&src、int start、int end)
{
std::载体ret;
对于(int i=start;i您拥有单元格[nc]
和节点[nd]
的位置将被破坏,因为这些向量的大小从未增加
这里有一些东西可以满足您的需要,而不需要std::stod
。我将其更改为使用ND行中的所有值,假设这是您的意思,如果不是,则将GetValues调用中的4更改为3
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
double ToDouble(const std::string& str)
{
std::stringstream s(str);
double d;
s >> d;
return d;
}
std::vector<double> GetValues(const std::vector<std::string>& src, int start, int end)
{
std::vector<double> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(ToDouble(src[i]));
}
return ret;
}
void PrintValues(const std::string& title, std::vector<std::vector<double>>& v)
{
std::cout << title << std::endl;
for(size_t line = 0; line < v.size(); ++line)
{
for(size_t val = 0; val < v[line].size(); ++val)
{
std::cout << v[line][val] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::vector<std::vector<std::string>> values;
std::ifstream fin("example.2dm");
for (std::string line; std::getline(fin, line); )
{
std::istringstream in(line);
values.push_back(
std::vector<std::string>(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>()));
}
std::vector<std::vector<double>> cells;
std::vector<std::vector<double>> nodes;
for (size_t i = 0; i < values.size(); ++i)
{
if(values[i][0] == "E3T")
{
cells.push_back(GetValues(values[i], 1, 5));
}
else if(values[i][0] == "E4Q")
{
cells.push_back(GetValues(values[i], 1, 6));
}
else if(values[i][0] == "ND")
{
nodes.push_back(GetValues(values[i], 1, 4));
}
}
PrintValues("Cells", cells);
PrintValues("Nodes", nodes);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
双ToDouble(const std::string和str)
{
std::strings(str);
双d;
s>>d;
返回d;
}
std::vector GetValues(const std::vector&src、int start、int end)
{
std::载体ret;
对于(int i=start;istd::stod问题很奇怪,我认为这在Cygwin上有效。如果使用-std=c++11
编译此程序会发生什么
#include <string>
#if __cplusplus < 201103L
#warning No C++11 support
#endif
#if !defined(_GLIBCXX_USE_C99)
#warning No C99 library functions
#endif
#if defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
#warning Broken vswprintf
#endif
int main() { }
#包括
#如果uu cplusplus<201103L
#警告:不支持C++11
#恩迪夫
#如果!已定义(\u GLIBCXX\u使用\u C99)
#警告:没有C99库函数
#恩迪夫
#如果定义(_GLIBCXX_HAVE_breaked_VSWPRINTF))
#警告:vswprintf已损坏
#恩迪夫
int main(){}
这些是libstdc++定义std::stod
所需的预处理器条件,因此,如果编译时产生任何警告,那么您将无法使用std::stod
。您可能需要使用strtod(值[i][3]。c_str(),nullptr)
相反。std::stod问题很奇怪,我认为这在Cygwin上有效。如果使用-std=c++11
编译此程序会发生什么
#include <string>
#if __cplusplus < 201103L
#warning No C++11 support
#endif
#if !defined(_GLIBCXX_USE_C99)
#warning No C99 library functions
#endif
#if defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
#warning Broken vswprintf
#endif
int main() { }
#包括
#如果uu cplusplus<201103L
#警告:不支持C++11
#恩迪夫
#如果!已定义(\u GLIBCXX\u使用\u C99)
#警告:没有C99库函数
#恩迪夫
#如果定义(_GLIBCXX_HAVE_breaked_VSWPRINTF))
#警告:vswprintf已损坏
#恩迪夫
int main(){}
这些是libstdc++定义std::stod
所需的预处理器条件,因此,如果编译时产生任何警告,那么您将无法使用std::stod
。您可能需要使用strtod(值[i][3]。c_str(),nullptr)
。std::stod
是c++11的东西,您需要