如何让我的程序识别 .txt 文件中的不同变量类型并对其进行排序?
How do I get my program to recognize and sort the different variable types from a .txt file?
对 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
如图所示,存在 int 变量和 char(A 和 B)的组合。使用 ifstream,程序可以很好地识别前三个 int (10, 4, 6)。但是,我试图找出一种方法将剩余的变量整理到特定的数组中。
例如,我希望它识别出每次出现 'A' 时,'A' 之后的第一个 int 进入一个数组,随后的 int 进入另一个数组。所以在这种情况下,我们将拥有数组 [15,54,73,80] 和 [6,12,30,10]。
请注意,input.txt 未设置,我需要让它能够读取具有不同数量的 A 和 B 的文件。
有很多方法可以解决这个问题,包括编写 boost::spirit 语法,但我不建议新接触 C++ 的人这样做。
相反,我会建议直接方法,它涉及一个 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;
}
使用此代码阅读 input.txt
(C++
)
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();
对 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
如图所示,存在 int 变量和 char(A 和 B)的组合。使用 ifstream,程序可以很好地识别前三个 int (10, 4, 6)。但是,我试图找出一种方法将剩余的变量整理到特定的数组中。
例如,我希望它识别出每次出现 'A' 时,'A' 之后的第一个 int 进入一个数组,随后的 int 进入另一个数组。所以在这种情况下,我们将拥有数组 [15,54,73,80] 和 [6,12,30,10]。
请注意,input.txt 未设置,我需要让它能够读取具有不同数量的 A 和 B 的文件。
有很多方法可以解决这个问题,包括编写 boost::spirit 语法,但我不建议新接触 C++ 的人这样做。
相反,我会建议直接方法,它涉及一个 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;
}
使用此代码阅读 input.txt
(C++
)
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();