将单个字符串分解为多个字符串C ++? [英] Breaking a single string into multiple strings C++?

查看:136
本文介绍了将单个字符串分解为多个字符串C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要输入3个全名,以逗号分隔

I need to input 3 full names separated by commas

全名1:约翰,史密斯,弗林

全名2:Walter, Kennedy,Roberts

全名3:Sam,Bass,Clinton

Full Name 1: John, Smith, Flynn
Full Name 2: Walter, Kennedy, Roberts
Full Name 3: Sam, Bass, Clinton

然后像这样输出它

名字1:约翰

名字2:沃尔特

名字3:Sam

First Name 1: John
First Name 2: Walter
First Name 3: Sam

中间名1:史密斯

中间名2:肯尼迪

中间名3:低音

Middle Name 1: Smith
Middle Name 2: Kennedy
Middle Name 3: Bass

姓氏1:Flynn

姓氏2:Roberts

姓氏3:克林顿

Last Name 1: Flynn
Last Name 2: Roberts
Last Name 3: Clinton

怎么办我做这些?到目前为止,
是我的代码

How do i do these? so far this is my code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main () {
    char first[3][100];
    char middle[3][100];
    char last[3][100];
    char full[3][100];
    int i; 

    cout << "Enter 3 Full Names <first, middle and last names separated by comma>:" << endl;
    for (i=0; i<3; i++) {
         cout << "Full Name " << i+1 << ":" ;
        gets (full[i]);          
    }

    cout << "The first names are: " << endl;
    for (i=0; i<3; i++) {
        strcpy (first[i], full[i]);
        if (strcmp (first[i], ", ")) {
            cout << "First Name "<< i+1 << ":" ;
            strcpy ( first[i], full[i] );
            cout << (first[i]);
            cout << endl;   
        }
    } 
    cout << "The middle names are: " << endl;
    for (i=0; i<3; i++) {
        cout << "Middle Name "<< i+1 << ":" ;
        cout << (middle[i]);
        cout << endl;
    }
    cout << "The last names are: " << endl;
    for (i=0; i<3; i++) {
        cout << "Last Name "<< i+1 << ":" ;
        cout << (last[i]);
        cout << endl;
    }
    system("pause");
    return 0;
}


推荐答案

我可能会做点什么像这样:

I'd probably do something like this:

编辑:经过一番思考,我决定重复确实对我造成了太大的困扰,因此我将其消除了。我不确定技术上是否允许使用它( std :: string 不是POD),但它似乎可以正常工作,并给我带来了更好,更可扩展的印象。

after some thought, I've decided the duplication really did bother me too much, so I've eliminated it. I'm not sure it's technically allowed (std::string isn't a POD) but it seems to work, and strikes me as nicer and more scalable.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <iterator>

struct name { 
    std::string first, middle, last;
};

std::istream &operator>>(std::istream &is, name &n) { 
    char ch;
    is.ignore((unsigned)-1, ':');
    is.ignore(1);

    std::getline(is, n.first, ',');
    std::getline(is, n.middle, ',');
    std::getline(is, n.last);
    return is;
}

struct item { 
    size_t offset;
    char *caption;
};

void show(name const &n, item const &i) { 
    // as predicted, eliminating the duplication did lead to one gnarly line of code:
    std::string &name = *(std::string *)((char *)&n+i.offset);
    std::cout << i.caption << name << "\n";
}

int main() {     
    std::vector<name> names;

    std::string raw_data("Full Name 1: John, Smith, Flynn\nFull Name 2: Walter, Kennedy, Roberts\nFull Name 3: Sam, Bass, Clinton");

    std::istringstream infile(raw_data);

    std::copy(std::istream_iterator<name>(infile),
              std::istream_iterator<name>(),
              std::back_inserter(names));

    item items[] = { 
        {offsetof(name, first), "First Name: "},
        {offsetof(name, middle), "Middle Name: "},
        {offsetof(name, last), "Last name: "} 
    };

    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++)
            show(names[j], items[i]);
        std::cout << "\n";
    }
    return 0;
}

这篇关于将单个字符串分解为多个字符串C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆