打印一次出现的字符串数组的第一个元素

时间:2018-12-23 23:53:22

标签: c++ arrays string

我有一个名称数组,我需要打印只出现一次的名字。例如,我有以下名称:

乔,安迪,阿尔伯特,安迪,泰勒,阿尔伯特。

程序应打印出Joe(如果没有正确答案,则打印出空白行),因为这是第一次出现一次。

到目前为止,这是我的程序:

#include <iostream>

using namespace std;

int main()
{
int size;
cin >> size;
string trash;

string arr[size];

for (int i=0; i<size; i++)
{
    cin >> arr[i];
}

getline(cin,trash);

string first;
for (int i=0; i<size; i++)
{
   if ( arr[i] != arr[i+1] )
    first = arr[i];
}

cout << first << endl;
}

4 个答案:

答案 0 :(得分:2)

这是我的版本:

#include <iostream>
using namespace std;
int main()
{
int size;
cin >> size;
// string trash;
string arr[size];
for (int i=0; i<size; i++){
    cin >> arr[i];
}
// getline(cin,trash);
string first;
for (int i=0; i<size; i++)
{
    first = arr[i];
    for (int j = 0; j < size; ++j){
        if ( arr[i] == arr[j] && i!=j)
            first = "";
    }
    if (first == arr[i])
        break;
}

cout << first << endl;
}

答案 1 :(得分:0)

使用c ++ stl可以使您的生活更轻松,如果您不了解任何部分,请告诉我。

下面是实现

#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<string>a(n);
    for(int i=0;i<n;i++)
        cin>>a[i];

    unordered_map<string,int>mapping; // to store the freqency of each unique string
                                      //actually we are intending to map each unique string to its frequency

    for(int i=0;i<n;i++)
    {
        mapping[a[i]]+=1; //incrementing the frequency of same string 
    }
    bool success=0;
    for(int i=0;i<n;i++)
    {
        if(mapping[a[i]]==1) //if first time we get a string whose frequency is 1 we print it and break out of the loop
            {
                cout<<a[i]<<"\n";
                success=1;
                break;
            }
    }
    if(success==0)
       cout<<"\n";


return 0;}

答案 2 :(得分:0)

我没有对此进行彻底的测试,但是我会选择这样的方法。 min_element算法的效果很好:

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

vector<string> testNames;

int main()
{
    testNames.push_back("Joe");
    testNames.push_back("Andy");
    testNames.push_back("Albert");
    testNames.push_back("Andy");
    testNames.push_back("Tyler");
    testNames.push_back("Albert");
    map<string, vector<int>> nameHits;

    for (size_t i = 0; i != testNames.size(); ++i)
        nameHits[testNames.at(i)].push_back(i);

    map<string, vector<int>>::const_iterator cIter;
    cIter = min_element(nameHits.cbegin(), nameHits.cend(), 
        [](const pair<string, vector<int>>& e1, const pair<string, vector<int>>& e2)
        { return e1.second.size() == 1 && e1.second.at(0) < e2.second.at(0); });

    if (cIter->second.size() != 1 || cIter == nameHits.end())
        cout << "";
    else
        cout << (*cIter).first;

    getchar();
}

答案 3 :(得分:-2)

重点是堆栈和堆的概念之间的区别。

对于动态大小的数组,请尝试std::vector

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int size;
    cin >> size;
    string trash;

    vector<string> arr;
    arr.assign(size,"");

    for (int i=0; i<size; i++)
    {
        cin >> arr[i];
    }

    getline(cin,trash);

    string first;
    for (int i=0; i<size; i++)
    {
        if ( arr[i] != arr[i+1] )
            first = arr[i];
    }

    cout << first << endl;
}
相关问题