C++ 如何按分隔符拆分字符串,并将每个字符串放入不同的向量中?

C++ 如何按分隔符拆分字符串,并将每个字符串放入不同的向量中?,c++,parsing,split,C++,Parsing,Split,我正在尝试获取一个包含3个子字符串的输入并对其进行解析: 城市名称、纬度和经度 例如:“蒙蒂塞洛36.8297222-84.8491667” 这样我就可以将城市、纬度和经度推回到它们自己的向量对象中。但我不知道该怎么做 vector<string> city; vector<float> latitude; vector<float> longitude; int main(int argc, char* argv[]){ fstream inDat

我正在尝试获取一个包含3个子字符串的输入并对其进行解析:

城市名称、纬度和经度

例如:
“蒙蒂塞洛36.8297222-84.8491667”

这样我就可以将城市、纬度和经度推回到它们自己的向量对象中。但我不知道该怎么做

vector<string> city;
vector<float> latitude;
vector<float> longitude;

int main(int argc, char* argv[]){
    fstream inData;
    inData.open(argv[1]);   // open the specified file for reading. 
    string line;
    if (!inData.fail())         // if the file exits and is opened successfully
    {
        while(getline(inData, line))            // read every line from the file until the end
        {
            //here is where I want to parse and pushback into it's vector
        }
    }
    inData.close();
矢量城市;
矢量纬度;
矢量经度;
int main(int argc,char*argv[]){
因达塔河;
open(argv[1]);//打开指定的文件进行读取。
弦线;
if(!inData.fail())//如果文件退出并成功打开
{
while(getline(inData,line))//读取文件中的每一行,直到结束
{
//这里是我想要解析并返回到它的向量的地方
}
}
inData.close();
可以接受一个附加参数,一个分隔符字符,默认设置为“\n”。实际上,您正在使用它逐行读取

在读了一行之后,您可以做的是将该行分配给
std::istringstream
的一个实例,并让
操作符>
按空格分割它(默认行为)

std::向量(3);
while(std::getline(inData,line)){
std::istringstream iss{line};
字符串标记;
尺寸i=0;
while(iss>>令牌){
向量[i++]。推回(令牌);
}
}

您可以使用stringstream(更好的是:istringstream)解析行并将结果写入变量;然后使用变量将它们推回到向量中:

while(getline(inData, line)) 
{
  std::istringstream ss (line);

  std::string city_s;
  float longitude_f;
  float latitude_f;

  if (ss >> city_s >> latitude_f >> longitude_f) {
    city.push_back(city_s);
    latitude.push_back(latitude_f);
    longitude.push_back(longitude_f);
  }
}

原始代码的重构和更完整版本。使用c++11

#include <vector>
#include <string>
#include <fstream>
#include <iostream>

struct place {
    std::string city;
    double latitude;
    double longitude;
};

std::vector<place> read_places(std::istream& is)
{
    place buffer;
    std::vector<place> places;
    while (is >> buffer.city >> buffer.latitude >> buffer.longitude) {
        places.push_back(std::move(buffer));
    }
    return places;
}

int main(int argc, char* argv[]) {

    // preconditions
    if (argc < 2) {
        std::cerr << "usage: myprog <inputfile>\n";
        std::exit(4);
    }

    std::fstream inData(argv[1]);
    if (!inData) { std::cerr << "failed to open " << argv[1]; std::exit(100); }

    auto places = read_places(inData);
    inData.close();

    // now use places
    // ...
}
#包括
#包括
#包括
#包括
结构场所{
字符串城市;
双纬度;
双经度;
};
std::矢量读取位置(std::istream&is)
{
放置缓冲区;
std::矢量位置;
while(is>>buffer.city>>buffer.latitude>>buffer.longitude){
位置。推回(标准::移动(缓冲));
}
返回地点;
}
int main(int argc,char*argv[]){
//前提条件
如果(argc<2){


std::cerr还可以使用字符串标记器-strtok()并使用“”作为分隔符。下面的链接可能会帮助您-

为什么不创建一个类,将这三个属性存储为成员,并使用该类类型的向量而不是3个平行向量?分隔符是一个空格?@richardhogesyes@NathanOliver我计划切换到结构,但我认为使用3种不同的向量更容易de>inData>>city\u name>>lati>>logi;
,然后将它们复制/移动到您的容器中。我知道如何将其拆分并将其推回1个向量。但这个问题的关键是将行拆分,并将每个元素推回到其自己的向量中。对不起,我不明白您可能希望现在再次删除关于使用getline的行您已经更改为使用istringstream
运算符>>
读取以空格分隔的值,因此使用
std::getline(iss,token'')和
iss>>令牌
谢谢各位,编辑后我忘了删除那个部分。在进行推送之前,您可能需要检查
操作符>
的结果:
如果(ss>>城市>>纬度f>>经度f){city.push_-back(城市);latitude.push_-back(纬度f);经度。push_-back(经度f)}
哪些向量包含值?结构?我能不能只做cout@GeorgeWisten是的。试试看。不起作用,错误:在(int I=0;Iplaces[I]。city中没有名为'city'的成员。places是数组,而不是city。它符合要求,但不打印任何内容
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

struct place {
    std::string city;
    double latitude;
    double longitude;
};

std::vector<place> read_places(std::istream& is)
{
    place buffer;
    std::vector<place> places;
    while (is >> buffer.city >> buffer.latitude >> buffer.longitude) {
        places.push_back(std::move(buffer));
    }
    return places;
}

int main(int argc, char* argv[]) {

    // preconditions
    if (argc < 2) {
        std::cerr << "usage: myprog <inputfile>\n";
        std::exit(4);
    }

    std::fstream inData(argv[1]);
    if (!inData) { std::cerr << "failed to open " << argv[1]; std::exit(100); }

    auto places = read_places(inData);
    inData.close();

    // now use places
    // ...
}