gpt4 book ai didi

C++ 读取制表符分隔的输入并跳过空白字段

转载 作者:太空狗 更新时间:2023-10-29 23:01:43 34 4
gpt4 key购买 nike

我正在制作一个程序,它将接收用户粘贴到控制台中的一长串以制表符分隔的元数据,并将它们拆分为正确的变量。我已经完成了按制表符拆分行的代码,但是为了将正确的元数据放入正确的字符串变量中,应该跳过一些空字段,但我无法开始工作。

这是我目前的代码:

string dummy;
string FAImport;
cin.ignore(1000, '\n');
cout << "\nPlease copy and paste the information from the finding aid and press Enter: ";
getline(cin, FAImport);
cout << FAImport;

stringstream ss(FAImport);

auto temp = ctype<char>::classic_table();
vector<ctype<char>::mask> bar(temp, temp + ctype<char>::table_size);

bar[' '] ^= ctype_base::space;

ss.imbue(locale(cin.getloc(), new ctype<char>(bar.data())));

ss >> coTitle >> altTitle >> description >> dateSpan >> edition >> publisher >>
physicalDescription >> scale >> extentField >> medium >> dimensions >> arrangement >>
degree >> contributing >> names >> topics >> geoPlaceNames >> genre >> occupations >>
functions >> subject >> langIN >> audience >> condition >> generalNotes >> collection >>
linkToFindingAid >> source >> SIRSI >> callNumber;

checkFAImport(); //shows the values of each variable
cout << "\n\nDone";

使用这段代码,我在输入元数据后得到了这个输出:

coTitle = William Gates photograph with Emiliano Zapata
altTitle = 1915
description = 1915
datespan = Electronic version
edition = 1 photograph : sepia ; 11 x 13 cm
publisher = L. Tom Perry Special Collections, Harold B. Lee Library, Brigham Young University
physicalDescription = Photographs
scale = William Gates papers
extentField = http://findingaid.lib.byu.edu/viewItem/MSS%20279/Series%2011/Subseries%205/Item%20979/box%20128/folder%2012
medium = William Gates photograph with Emiliano Zapata; MSS 279; William Gates papers; L. Tom Perry Special Collections; 20th Century Western & Mormon Manuscripts; 1130 Harold B. Lee Library; Brigham Young University; Provo, Utah 84602; http://sc.lib.byu.edu/
dimensions = MSS 279 Series 11 Subseries 5 Item 979 box 128 folder 12
arrangement =
degree =
contributing =
names =
topics =
geoPlaceNames =
genre =
occupations =
functions =
subject =
langIN =
audience =
condition =
generalNotes =
collection =
linkToFindingAid =
source =
SIRSI =
callNumber =

在此示例中,altTitle 和 description 等字段应留空并跳过。任何帮助将不胜感激。

最佳答案

您已经优雅地解决了字段中的空格问题。不幸的是,operator>> 将跳过连续的制表符,就好像它们是一个单独的分隔符一样。那么,再见空旷的田野?

一种简单的方法是使用 getline() 读取单个字符串字段:

getline (ss, coTitle, '\t'); 
getline (ss, altTitle, '\t');
getline (ss, description, '\t');
...

另一种方式是

关于C++ 读取制表符分隔的输入并跳过空白字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30494022/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com