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

Print first element of a string array which occurs one time

本文关键字:字符串 数组 第一个 元素 一次 打印      更新时间:2023-10-16

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

乔,安迪,阿尔伯特,

安迪,泰勒,阿尔伯特。

程序应该打印出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;
}
这是我

的版本:

#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;
}
我没有

彻底测试,但我会选择这样的方法。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();
}

好吧,使用 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;}

关键是堆栈和堆概念之间的区别。

对于动态大小的数组,请尝试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;
}