C++ 在C+中将字符向量转换为字符串向量+;
对于以下内容,我需要返回字符串向量,其中向量中的每个元素的长度为1。我有一个答案,可以生成一个字符向量。我想它可以满足我的需要,但我不确定如何将结果作为长度为1的字符串向量返回。请参阅下面我的实现。注意,我不能更改函数的返回类型,它必须是字符串向量。考虑到我在解决方案中对字符进行操作,我不确定如何更改它C++ 在C+中将字符向量转换为字符串向量+;,c++,string,vector,char,C++,String,Vector,Char,对于以下内容,我需要返回字符串向量,其中向量中的每个元素的长度为1。我有一个答案,可以生成一个字符向量。我想它可以满足我的需要,但我不确定如何将结果作为长度为1的字符串向量返回。请参阅下面我的实现。注意,我不能更改函数的返回类型,它必须是字符串向量。考虑到我在解决方案中对字符进行操作,我不确定如何更改它 vector<string> commonChars(vector<string>& A) { vector<char> re
vector<string> commonChars(vector<string>& A) {
vector<char> resVec;
unordered_map<char, int> mapObj;
for (const auto& str : A) {
for (const auto& letter : str) {
mapObj[letter]++;
}
}
int sizeOfInput = A.size();
for (int i{}; i < A[0].size(); i++) {
if (!mapObj.count(A[0][i])) continue;
resVec.insert(resVec.begin(), mapObj[(A[i])] / sizeOfInput, A[i]);
}
return resVec;
}
vector常见字符(vector&A){
载体resVec;
无序地图mapObj;
用于(常量自动和str:A){
用于(常数自动和字母:str){
mapObj[字母]+;
}
}
int-sizeOfInput=A.size();
对于(int i{};i
向量ret;
用于(字符ch:resVec)
后推(字符串(1,ch));
返回ret;
这是一种将std::vector
转换为std::vector
的简单方法。
如果您感兴趣,还可以查看:
简而言之,string(1,ch)
是实际转换的对象。std::vector resVec;
...
std::vector returnVec;
returnVec.reserve(resVec.size());
用于(字符ch:resVec){
returnVec.push_back(标准::字符串(1,ch));
//或:返回向量放置回(1,ch);
}
returnVec;
或者:
std::vector resVec;
...
std::vector returnVec;
returnVec.reserve(resVec.size());
std::transform(resVec.begin()、resVec.end()、std::back_插入器(returnVec),
[](char ch){return std::string(1,ch);}
);
returnVec;
或者:
std::vector resVec;
...
std::vector returnVec(resVec.size());
std::transform(resVec.begin()、resVec.end()、returnVec.begin(),
[](char ch){return std::string(1,ch);}
);
returnVec;
也就是说,您可以完全消除std::vector
,直接从std::map
数据填充最终的std::vector
:
std::vector commonChars(std::vector&A){
std::vector resVec;
std::无序地图mapObj;
用于(常量自动和str:A){
用于(常数自动和字母:str){
mapObj[字母]+;
}
}
size_t sizeOfInput=A.size();
如果(sizeOfInput>0){
for(常量自动和字母:A[0]){
自动iter=mapObj.find(字母);
if(iter!=mapObj.end()){
insert(resVec.begin(),iter->second/sizeOfInput,std::string(1,字母));
}
}
}
返回resVec;
}
解决这个问题有不同的方法。您可以为每个字符串计算向量中每个字母a-z的出现次数,并循环遍历每个向量,将字符在所有字符串中出现的最小次数相加:
vector<string> commonChars(vector<string>& A) {
const int maxTimes=100;
vector<string> resVec;
vector<vector<int>> counts;
counts.reserve(A.size());
//Count occurrences of letters in each string
for (const auto& word : A) {
vector<int> count(26, 0);
for (const auto letter : word) {
count[letter-'a']++;
}
counts.push_back(count);
}
//Add minimum number of times a letter occurs in all strings
for (int i=0; i<26; ++i)
{
int min=maxTimes;
for (auto &count : counts)
{
if (count[i] < min) {
min = count[i];
}
}
for (int j=0; j<min; ++j) {
resVec.push_back(string(1, i+'a'));
}
}
return resVec;
}
vector常见字符(vector&A){
常量int最大次数=100;
载体resVec;
病媒计数;
计数。保留(A.大小());
//计算每个字符串中出现的字母数
for(const auto&word:A){
病媒计数(26,0);
for(常量自动字母:word){
计数[字母-'a']++;
}
计数。推回(计数);
}
//添加字母在所有字符串中出现的最小次数
对于(int i=0;我可能是伟大答案的副本!谢谢。
vector<string> commonChars(vector<string>& A) {
const int maxTimes=100;
vector<string> resVec;
vector<vector<int>> counts;
counts.reserve(A.size());
//Count occurrences of letters in each string
for (const auto& word : A) {
vector<int> count(26, 0);
for (const auto letter : word) {
count[letter-'a']++;
}
counts.push_back(count);
}
//Add minimum number of times a letter occurs in all strings
for (int i=0; i<26; ++i)
{
int min=maxTimes;
for (auto &count : counts)
{
if (count[i] < min) {
min = count[i];
}
}
for (int j=0; j<min; ++j) {
resVec.push_back(string(1, i+'a'));
}
}
return resVec;
}