如何让程序从.txt文件中识别和排序不同的变量类型? 对C++(通常是编程)来说是相当新的。我正在尝试编写一个程序,该程序将读取以下input.txt: 10 4 6 A 15 6 B 20 10 B 12 8 A 54 12 B 64 9 A 73 30 A 80 10 B 99 15

如何让程序从.txt文件中识别和排序不同的变量类型? 对C++(通常是编程)来说是相当新的。我正在尝试编写一个程序,该程序将读取以下input.txt: 10 4 6 A 15 6 B 20 10 B 12 8 A 54 12 B 64 9 A 73 30 A 80 10 B 99 15,c++,arrays,ifstream,C++,Arrays,Ifstream,如图所示,有int变量和char(a和B)的组合。使用ifstream,程序可以很好地识别前三个int(10,4,6)。然而,我正试图找出一种方法,让它将剩余的变量分类到特定的数组中 例如,我想让它认识到,每当有一个'A',在'A'之后的第一个int进入一个数组,接下来的int进入另一个数组。在这种情况下,我们将得到数组[15,54,73,80]和[6,12,30,10] 请注意,输入不设置,我需要能够读取不同的As和B的文件。 < P>有很多方法来解决这个问题,包括编写一个Booo::精神语法

如图所示,有int变量和char(a和B)的组合。使用ifstream,程序可以很好地识别前三个int(10,4,6)。然而,我正试图找出一种方法,让它将剩余的变量分类到特定的数组中

例如,我想让它认识到,每当有一个'A',在'A'之后的第一个int进入一个数组,接下来的int进入另一个数组。在这种情况下,我们将得到数组[15,54,73,80]和[6,12,30,10]


请注意,输入不设置,我需要能够读取不同的As和B的文件。

< P>有很多方法来解决这个问题,包括编写一个Booo::精神语法,但是我不推荐给C++的新的人。p> 相反,我将建议采用直接方法,其中包括if语句:

- instead of streaming into an int, stream into a string.
- for each string (word), check if word is A or B,  if so add to your char container.  otherwise call stoi and add to your int container.

在这里,您编写示例的速度相当快,因此可以更好地完成某些部分,例如检查字符串是否为数字,或者使用太多的向量etd

#include <vector>
#include <memory>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <iterator>
#include <locale>

struct Data
{
    std::string key;
    std::vector< int > v;
};

bool isNumber( const std::string& s )
{
    if( s.empty() )
    {
        return false;
    }
    std::locale loc;
    std::string::const_iterator it = s.begin();
    while( it != s.end() )
    {
        if( !std::isdigit( *it, loc) )
        {
            return false;
        }
        ++it;
    }
    return true;
}

int main(int argc, char *argv[])
{
    std::ifstream file;
    file.open( "/home/rwadowski/file.txt" );
    std::map< std::string, Data > map;
    if( file.is_open() )
    {
        std::string line;
        while( std::getline( file, line ) )
        {
            std::istringstream stream( line );
            std::istream_iterator< std::string > it( stream );
            std::istream_iterator< std::string > end;
            std::vector< std::string > strings;
            while( it != end )
            {
                strings.push_back( *it );
                ++it;
            }
            std::string key;
            std::vector< int > v;
            for( const std::string& s : strings )
            {
                if( isNumber( s ) )
                {
                    v.push_back( std::stoi( s ) );
                }
                else
                {
                    key = s;
                }
            }
            Data data = map[ key ];
            data.v.insert( data.v.end(), v.begin(), v.end() );
            map[ key ] = data;
        }
        file.close();
    }

    std::map< std::string, Data >::iterator i = map.begin();
    while( i != map.end() )
    {
        std::cout << "Key[" << i->first << "] = ";
        for( int j = 0; j < i->second.v.size(); ++j )
        {
            std::cout << i->second.v[ j ] << " ";
        }
        std::cout << std::endl;
        ++i;
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构数据
{
std::字符串键;
std::vectorv;
};
布尔isNumber(常量标准::字符串和s)
{
如果(s.empty())
{
返回false;
}
std::locale loc;
std::string::const_迭代器it=s.begin();
while(it!=s.end())
{
如果(!std::isdigit(*it,loc))
{
返回false;
}
++它;
}
返回true;
}
int main(int argc,char*argv[])
{
std::ifstream文件;
打开(“/home/rwadowski/file.txt”);
std::mapmap;
if(file.is_open())
{
std::字符串行;
while(std::getline(文件,行))
{
std::istringstream(线);
std::istream_迭代器it(流);
std::istream_迭代器end;
std::vectorstrings;
while(it!=结束)
{
字符串。将_向后推(*it);
++它;
}
std::字符串键;
std::vectorv;
for(const std::string&s:strings)
{
如果(ISN编号)
{
v、 推回(标准::stoi(s));
}
其他的
{
键=s;
}
}
数据=地图[键];
data.v.insert(data.v.end(),v.begin(),v.end());
映射[键]=数据;
}
file.close();
}
std::map::迭代器i=map.begin();
while(i!=map.end())
{

std::cout使用此代码读取
input.txt
C++

std::ifstream文件(“input.txt”);
如果(!file.is_open()){
cerr>m>>n>>o;
而(文件>>r>>p>>q){
如果(r=='A'){
a、 推回(p);
b、 推回(q);
c、 推回(r);
}
}
file.close();

为什么不使用std::map?(很抱歉,我没有时间写出完整的答案,但您可能应该查看标准库中名为std::map的内容)谢谢你的回答,不用担心。我这样做是为了一个项目,教授从来没有提到过std::map,所以我不认为我需要使用它。有没有一种方法可以使用for或while让它在整个txt文件中运行?读取“a”,并将其与Ascii值(65)进行比较。类似于B或任何字母表。有点累了,但举例说明了解决方案。std::map是解决此问题的最简单方法。我一直在按照这些思路思考。因此,我可以编写一些代码,识别每个“A”的Ascii值,并将以下两个整数输入到相应的数组中?感谢您的帮助和建议!但是,这用于学校项目,我不允许使用std::stoi或boost::spirit语法。我考虑的几个选项是让程序比较ascii值(如上所述)另一个建议是GET方法,但我不太熟悉。你认为怎么样?考虑到STD::Sti是C++标准的一部分,如果你不允许使用它,我会感到非常惊讶。我不推荐使用SLIP,因为使用它至少需要中级C++知识。或者给出你可以使用和不能使用的特定条件?比较ascii值适用于你处理的情况(可能是基本ascii)在重新阅读你的问题之后,我意识到你只需要检测字符A或B,其他的你就有一个int。这大大简化了问题,所以我已经提出了我的答案。如果你对C++中的哪些部分有限制,你需要把它添加到GIK中。假设。即使std::stoi是一个标准,我还没有了解它,所以我认为我们不允许使用它。我非常确定我们已经了解了解决这个问题所必需的基础知识。我们最近学习了结构和类,但我认为这对这一点没有帮助。顺便说一句,让它识别“a”和“a”会更容易吗只需将字符后面的两个数字放在一个数组中?我在想,我可以自己使用for循环和模运算符将数字排序到单独的数组中。再次感谢您的帮助。这对具体示例有效,但不适用于所述的问题,即将任意整数和字符串解析为整数或整数串。
std::ifstream file("input.txt");
if (!file.is_open()) {
    cerr << "Error\n";
    return 1;
}
int m, n, o, p, q;
char r;
std::vector<int> a, b;
std::vector<char> c;
file >> m >> n >> o;
while (file >> r >> p >> q) {
    if (r == 'A') {
        a.push_back(p);
        b.push_back(q);
        c.push_back(r);
    }
}
file.close();