Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 打印一次出现的字符串数组的第一个元素_C++_Arrays_String - Fatal编程技术网

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

C++ 打印一次出现的字符串数组的第一个元素,c++,arrays,string,C++,Arrays,String,我有一个带有名字的数组,我需要打印出只出现一次的名字。例如,我有以下姓名: 乔,安迪,艾伯特,安迪,泰勒,艾伯特 程序应该打印Joe(如果没有正确的答案,那么打印一个空行),因为这是第一个出现一次的答案 以下是我目前的计划: #include <iostream> using namespace std; int main() { int size; cin >> size; string trash; string arr[size]; for (int i=0

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

乔,安迪,艾伯特,安迪,泰勒,艾伯特

程序应该打印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;
}
#包括
使用名称空间std;
int main()
{
整数大小;
cin>>尺寸;
字符串垃圾;
字符串arr[大小];
对于(int i=0;i>arr[i];
}
getline(cin,垃圾);
先串;

对于(inti=0;i而言,重点在于堆栈和堆概念之间的区别

对于动态大小的数组,请尝试
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;
}
#包括
#包括
使用名称空间std;
int main()
{
整数大小;
cin>>尺寸;
字符串垃圾;
向量arr;
arr.assign(大小“”);
对于(int i=0;i>arr[i];
}
getline(cin,垃圾);
先串;
对于(int i=0;i,以下是我的版本:

#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;
}
#包括
使用名称空间std;
int main()
{
整数大小;
cin>>尺寸;
//字符串垃圾;
字符串arr[大小];
对于(int i=0;i>arr[i];
}
//getline(cin,垃圾);
先串;

对于(int i=0;i),用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;
int main()
{
int n;
cin>>n;
向量(n);
对于(int i=0;i>a[i];
无序映射;//存储每个唯一字符串的频率
//实际上,我们打算将每个独特的字符串映射到它的频率

对于(int i=0;i我没有彻底测试它,但我会选择这样的方法。min_元素算法运行良好:

#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();
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
向量测试名;
int main()
{
testNames.push_back(“乔”);
testNames.push_back(“安迪”);
testNames.push_back(“阿尔伯特”);
testNames.push_back(“安迪”);
testNames.push_back(“泰勒”);
testNames.push_back(“阿尔伯特”);
地图名称;
对于(size_t i=0;i!=testNames.size();++i)
名称点击[testNames.at(i)]。向后推(i);
map::const_迭代器cIter;
cIter=min_元素(nameHits.cbegin(),nameHits.cend(),
[](常数对和e1、常数对和e2)
{return e1.second.size()==1&&e1.second.at(0)second.size()!=1 | | cIter==nameHits.end())

CUT变量长度数组不是标准C++。这是你的第一个问题。如果我要对这个问题进行排序,我会因为这个原因而失败,完全停止。第二个问题是,当然,搜索逻辑完全被破坏了。逻辑包括一个循环,它总是把一个元素和下一个元素进行比较。因此,数组是“鲍伯,爱丽丝,鲍伯”。当然,这两个Bob之间的比较总是在错误的情况下,比较起来总是在相邻元素之间,当然,这是完全错误的。首先需要重新思考,并用短句子写下正确的算法。然后将其转换成C++。st=”“;
。如果找到相同的名称,则无需继续。