C++ 结构数组,非常量大小

C++ 结构数组,非常量大小,c++,arrays,struct,C++,Arrays,Struct,如果需要根据所使用的文件更改数组的大小,是否可以创建结构数组 我正在创建一个结构数组,但我正在从文件填充结构。我需要根据文件中的行数确定数组的大小 ----好的,谢谢,这是我正在做的一个附带项目,在学校里没有使用向量。---因为你在学校里还没有学习过标准库,这里有一个演示如何使用标准库从文本文件创建一个行数组(astd::vector),以及如何处理故障 该守则并不实用对于实际代码,我只需在每次迭代中使用一个循环,getline,然后在向量上push\u。 希望这能为您提供一些这方面的想法,并显

如果需要根据所使用的文件更改数组的大小,是否可以创建结构数组

我正在创建一个结构数组,但我正在从文件填充结构。我需要根据文件中的行数确定数组的大小


----好的,谢谢,这是我正在做的一个附带项目,在学校里没有使用向量。---

因为你在学校里还没有学习过标准库,这里有一个演示如何使用标准库从文本文件创建一个行数组(a
std::vector
),以及如何处理故障

该守则并不实用对于实际代码,我只需在每次迭代中使用一个循环,
getline
,然后在向量上
push\u

希望这能为您提供一些这方面的想法,并显示需要哪些标题来完成哪些任务。:)

#包括
使用std::copy;
#包括
使用std::cout;使用std::cerr;使用std::istream;
#包括
使用std::ifstream;
#包括
使用std::exception;使用std::runtime\u错误;
#包括//EXIT_xxx
#包括
使用std::string;
#包括
使用std::vector;
#包括
使用std::back_插入器;使用std::istream_迭代器;
自动(布尔常数)->bool{returne;}
自动失败(字符串常量和消息)->bool{throw runtime_error(消息);}
班级线
{
私人:
字符串字符;
公众:
运算符字符串&(){返回字符}
运算符字符串常量&()常量{返回字符}
//运算符字符串&&()&&{return chars_;}
朋友
自动操作员>>(istream&stream、Line&Line)
->峡流&
{
getline(stream,line.chars);
希望(stream.eof()或不是stream.fail())
||失败(“使用getline()读取一行失败”);
回流;
}
};
void cppmain(字符常量文件名[])
{
iff流(文件名);
希望(不是f.fail())
||失败(“无法打开指定的文件”);
//这是从文本文件创建行数组的一种方法:
矢量线;
使用In_it=istream_迭代器;
复制(在it(f)中,在it()中,返回插入器(行));
用于(字符串常量和字符串:行)
{

CUT使用了 STD::向量。Victor.and在学校没有使用向量——这是C++在学校中被教导的悲哀形状。我怀疑C++在学校里是如何被教的:“现在,打开你的浏览器,键入www. StaskOfFuff.com,点击‘问一个问题’,剪切和粘贴你的代码,然后等着别人来帮助你”。。您可以使用
new
执行动态分配。您还可以实现一个链表类。匿名向下投票人:如果您能解释您的向下投票,这将有助于其他人(了解您的见解,或者更容易忽略您)。就目前而言,这是一个小孩子的交流方式。比如说,让它更成熟一点如何?
#include <algorithm>
using std::copy;

#include <iostream>
using std::cout; using std::cerr; using std::istream;

#include <fstream>
using std::ifstream;

#include <stdexcept>
using std::exception; using std::runtime_error;

#include <stdlib.h>     // EXIT_xxx

#include <string>
using std::string;

#include <vector>
using std::vector;

#include <iterator>
using std::back_inserter; using std::istream_iterator;

auto hopefully( bool const e ) -> bool { return e; }
auto fail( string const& message ) -> bool { throw runtime_error( message ); }

class Line
{
private:
    string  chars_;

public:
    operator string& () { return chars_; }
    operator string const& () const { return chars_; }
    //operator string&& () && { return chars_; }

    friend
    auto operator>>( istream& stream, Line& line )
        -> istream&
    {
        getline( stream, line.chars_ );
        hopefully( stream.eof() or not stream.fail() )
            || fail( "Failed reading a line with getline()" );
        return stream;
    }
};

void cppmain( char const filename[] )
{
    ifstream f( filename );
    hopefully( not f.fail() )
        || fail( "Unable to open the specified file." );

    // This is one way to create an array of lines from a text file:
    vector<string> lines;
    using In_it = istream_iterator<Line>;
    copy( In_it( f ), In_it(), back_inserter( lines ) );

    for( string const& s : lines )
    {
        cout << s << "\n";
    }
}

auto main( int n_args, char** args )
    -> int
{
    try
    {
        hopefully( n_args == 2 )
            || fail( "You must specify a (single) file name." );
        cppmain( args[1] );
        return EXIT_SUCCESS;
    }
    catch( exception const& x )
    {
        cerr << "!" << x.what() << "\n";
    }
    return EXIT_FAILURE;
}