我试图用C ++制作一本字典,但却无法在' ='上分割文本文件。分隔符。理想情况下,它将是两个数组。我想采取' ='的左侧。行的数组[0]和右侧的数组[1],然后使用数组[0]作为键和数组[1]作为通过预制插入函数的值,例如。 dictionary.insert(array [0],array [1])。我已经构建了字典逻辑但是在分割行时遇到了麻烦。
这是我的(可怕的)代码,它没有使用等号作为分隔符,因此将' ='到数组[1]:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace demogrid
{
public partial class grid : System.Web.UI.Page
{
GridView gvEmployee = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
BindData(table);
}
protected void BindData(DataTable dt)
{
gvEmployee.AllowPaging = true;
gvEmployee.PageSize = 2;
gvEmployee.AutoGenerateColumns = false;
gvEmployee.PageIndexChanging += gvEmployee_PageIndexChanging;
if (dt != null)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField boundfield = new BoundField();
boundfield.DataField = dt.Columns[i].ColumnName.ToString();
boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
gvEmployee.Columns.Add(boundfield);
}
Panel1.Controls.Add(gvEmployee);
gvEmployee.DataSource = dt;
gvEmployee.DataBind();
gvEmployee.Width = 600;
gvEmployee.HeaderStyle.CssClass = "header";
gvEmployee.RowStyle.CssClass = "rowstyle";
}
}
void gvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmployee.PageIndex = e.NewPageIndex;
gvEmployee.DataBind();
}
}
}
这是文本文件的前几行:
int main() {
Dictionary englishToEsperanto;
ifstream infile("Dictionary.txt");
string line;
string arr[2];
if (infile.is_open())
{
while (getline(infile, line))
{
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 2) {
ssin >> arr[i];
++i;
}
for (i = 0; i < 2; i++) {
cout << arr[i] << ' ';
}
cout << endl;
}
infile.close();
}
else
{
cout << "Error opening file";
}
return 0;
}
感谢您的光临。
答案 0 :(得分:2)
正如@Thomas在评论中所建议的那样,将'='
设置为第一个getline
的分隔符,将默认分隔符换行符设置为第二个getline
。 Demo.
string before_equal, after_equal;
string arr[2];
while (getline(cin, before_equal, '=') && getline(cin, after_equal))
{
stringstream ssin1(before_equal);
stringstream ssin2(after_equal);
if (ssin1.good() && ssin2.good()) {
ssin1 >> arr[0];
ssin2 >> arr[1];
}
else continue;
cout << arr[0] << ' ' << arr[1];
cout << endl;
}
答案 1 :(得分:1)
您可以使用类std::string
的标准数据成员函数来执行任务。结果对可以存储在标准类std::vector
这是一个示范程序
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <cstring>
std::string & trim( std::string &s )
{
std::string::size_type n = 0;
while ( n < s.size() && std::isspace( ( unsigned char )s[n] ) ) n++;
s.erase( 0, n );
n = s.size();
while ( n != 0 && std::isspace( ( unsigned char )s[n-1] ) ) n--;
s.erase( n );
}
int main()
{
const char * s[] =
{
"aback, to take = surprizi.",
"abaft = posta parto.",
"abandon = forlasi.",
"abase = humiligi. [error in book: humilgi]",
"abash = hontigi.",
"abate (lower) = mallevi.",
"abate (speed) = malakceli.",
"abbey = abatejo.",
"abbot = abato.",
"abbreviate = mallongigi.",
"abdicate = demeti la reĝecon.",
"abdomen = ventro.",
};
std::vector< std::pair<std::string, std::string>> dictionary;
for ( std::string line : s )
{
auto n = line.find( '=' );
std::string key, description;
if ( n == std::string::npos )
{
key = line;
}
else
{
key = line.substr( 0, n );
description = line.substr( n + 1 );
}
trim( key ); trim( description );
dictionary.push_back( { key, description } );
}
for ( const auto &p : dictionary )
{
std::cout << p.first << '\t' << p.second << std::endl;
}
return 0;
}
它的输出是
aback, to take surprizi.
abaft posta parto.
abandon forlasi.
abase humiligi. [error in book: humilgi]
abash hontigi.
abate (lower) mallevi.
abate (speed) malakceli.
abbey abatejo.
abbot abato.
abbreviate mallongigi.
abdicate demeti la reĝecon.
abdomen ventro.
答案 2 :(得分:0)
如果您有权访问Boost,Boost.Tokenizer就是您要找的。 您可以找到示例here。 这将返回一个字符串向量。
否则,您可以使用std::string::find,并按照以下方式实施:
int indOfDelimiter=line.find('=');
std::string Key = line.substr(0,indOfDelimiter);
std::string Value = line.substr(indOfDelimiter+1);
在您的示例中,'='之前和之后都有空格。您可能希望通过更改上面代码中的分隔符来删除它们:
const char *delimiter = " = ";
int indOfDelimiter=line.find(delimiter);
std::string Key = line.substr(0,indOfDelimiter);
std::string Value = line.substr(indOfDelimiter+3);