【问题标题】:Breaking a single string into multiple strings C++?将单个字符串分解为多个字符串 C++?
【发布时间】:2011-02-23 08:04:40
【问题描述】:

我需要输入3个用逗号分隔的全名

全名 1:约翰、史密斯、弗林
全名 2:沃尔特、肯尼迪、罗伯茨
全名 3:山姆、巴斯、克林顿

然后这样输出

名字 1:约翰
名字 2:沃尔特
名字 3:山姆

中间名 1:史密斯
中间名 2:肯尼迪
中间名 3:贝斯

姓 1:弗林
姓氏 2:罗伯茨
姓氏 3:克林顿

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

#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;
}

【问题讨论】:

  • 标签[作业]在哪里?
  • 我们不允许使用vector 只是iostream、cstring和cstdio!有人可以发布一个单个字符串的示例,然后将其分成 3 个多个字符串吗?我只是学习 C++,所以请简化一下谢谢。

标签: c++ string


【解决方案1】:

我想这里你想要的是一个字符串类的拆分方法,方法应该是这样的:

void SplitName(const string& fullName, const string& delims, vector<string>& names)
{
    size_t i = 0;
    size_t j = 0;
    while (i < fullName.size())
    {
        i = fullName.find_first_not_of(delims, i);
        j = fullName.find_first_of(delims, i);
        if (i < fullName.size())
        {
            names.push_back(fullName.substr(i, j - i));
        }
        i = j;
    }
}

您可以将“:”定义为分隔符,然后 names[1] 是 First Name,names[2] 是 Middle Name,names[3] 是 Last Name。

【讨论】:

  • 我们只允许使用:#include #include #include
【解决方案2】:

我可能会这样做:

编辑:经过一番思考,我认为重复确实让我很困扰,所以我已经消除了它。我不确定它在技术上是否允许(std::string 不是 POD),但它似乎可以工作,并且让我觉得它更好、更具可扩展性。

#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;
}

【讨论】:

  • 我们只允许使用:#include #include #include
  • 但是你为什么不使用指向成员的指针呢?您使用偏移量的方式看起来像是在尝试手动重新实现指向成员的指针。
  • @AnT:五年前,我可能会有答案,但到了这么晚的时候,我真的不记得了。
【解决方案3】:
void DumpName(char*  pBuffer, char cDelimiter, int iCounter);

int _tmain(int argc, _TCHAR* argv[])
{
    char full[3][100];

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

    for(int i=0; i<3; i++)
    {
        cout << "First Name "<< i+1 << ":" ;
        DumpName(full[i], ',', 0);
    }

    for(int i=0; i<3; i++)
    {
        cout << "Middle Name "<< i+1 << ":" ;
        DumpName(full[i], ',', 1);
    }

    for(int i=0; i<3; i++)
    {
        cout << "Last Name "<< i+1 << ":" ;
        DumpName(full[i], ',', 2);
    }

    return 0;
}

void DumpName(char*  pBuffer, char cDelimiter, int iCounter)
{
    int iFound = 0;

    char* pStart = pBuffer;
    char* pCurrent = pBuffer;
    while(*pCurrent != '\0')
    {
        if(*pCurrent == cDelimiter)
        {
            if(iFound == iCounter)
            {
                *pCurrent = '\0';
                cout << pStart << endl;
                *pCurrent = cDelimiter;
                return;
            }

            pStart = pCurrent;
            pStart ++;
            iFound ++;
        }

        pCurrent ++;
    }

    if((iCounter - iFound) == 0)
        cout << pStart << endl;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多