打印出现一次的字符串数组的第一个元素
Print first element of a string array which occurs one time
我有一个带有名称的数组,我需要打印出只出现一次的名字。例如,我有以下名称:
乔,安迪,阿尔伯特,安迪,泰勒,阿尔伯特。
程序应该打印出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;
}
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组