C++ 如何在C++;?
我正在为一个类编写一些代码,该类要求我以字符串形式输出重复项。此字符串可以有任何ascii字符,但输出只需显示重复的字符及其重复的总次数 下面是一些示例输入和输出 妈妈,m:2 塔可,没有重复的 干得好,o:3 塔科特,t:2C:2A:2 我的代码适用于除最后一个测试用例外的所有测试用例,t:2和a:2出现两次,现在我得出结论,我需要将重复字符存储在某个位置,并对该列表进行检查,以查看是否已打印该重复字符,因此我尝试使用向量 我的方法是在打印副本时将字符推入向量,如果字符已经在向量中,则在打印时跳过该字符。但我一直没能找到一个办法。我尝试使用C++ 如何在C++;?,c++,string,C++,String,我正在为一个类编写一些代码,该类要求我以字符串形式输出重复项。此字符串可以有任何ascii字符,但输出只需显示重复的字符及其重复的总次数 下面是一些示例输入和输出 妈妈,m:2 塔可,没有重复的 干得好,o:3 塔科特,t:2C:2A:2 我的代码适用于除最后一个测试用例外的所有测试用例,t:2和a:2出现两次,现在我得出结论,我需要将重复字符存储在某个位置,并对该列表进行检查,以查看是否已打印该重复字符,因此我尝试使用向量 我的方法是在打印副本时将字符推入向量,如果字符已经在向量中,则在打印时
#include中的find()
&我查看了它们,但它们不匹配,当我尝试应用它时,它完全破坏了我的代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector <char> alreadyprintedcharacters;
void findrepeats(string const&);
int main()
{
string input;
cout << "Enter the input : ";
getline(cin, input);
findrepeats(input);
return 0;
}
void findrepeats(string const &in)
{
int trackerOfDuplicates = 0;
int asciiArray[256];
char ch;
int charconv;
for (int i = 0; i < 256; i++) // creates my refference array for the comparison and sets all the values equal to zero
asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
if (asciiArray[charconv] == 0)
{
asciiArray[charconv] = 1;
}
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
}
bool trip = false;
for (unsigned int i = 0; i < in.length(); i++)
{
char static alreadyprinted;
char ch = in[i];
if ((asciiArray[ch] > 1) && (ch != alreadyprinted) && (find(alreadyprintedcharacters.begin(), alreadyprintedcharacters.end(), ch)!= alreadyprintedcharacters.end()))// change reflected HERE
{
cout << in[i] << " : " << asciiArray[ch] << endl;//???? maybe a nested loop
trip = true;
alreadyprinted = ch;
alreadyprintedcharacters.push_back(alreadyprinted);
}
}
if (trip == false)
cout << "No repeated characters were found.\n";
}
#包括
#包括
#包括
#包括
使用名称空间std;
矢量已打印字符;
void findrepeats(字符串常量&);
int main()
{
字符串输入;
cout(0)
{
asciiArray[charconv]=asciiArray[charconv]+;
}
}
bool-trip=false;
for(无符号整数i=0;i1)&&&(ch!=alreadyprinted)&&(find(alreadyprintedcharacters.begin(),alreadyprintedcharacters.end(),ch)!=alreadyprintedcharacters.end())//此处反映的更改
{
如果您修复了与std::find
:
std::find
不返回bool
,它返回一个迭代器(在您的例子中,是std::vector::iterator
)。如果要检查std::find
是否找到了某个内容,应该将其与alreadyprintedcharacters.end()进行比较
,因为这就是std::find
在未找到某个内容时返回的结果。您可以创建一个256的整数数组,并首先将其初始化为0。然后循环字符串中的字符,并递增与该字母对应的每个索引。最后,您可以打印出值大于1的字母。只需更改y即可我们的findrepeats功能如下:
void findrepeats(string const &in)
{
int asciiArray[256];
char ch;
int charconv;
bool foundAny = false;
for (int i = 0; i < 256; i++) asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
for (unsigned int i = 0; i < 256; i++)
{
char static alreadyprinted;
if (asciiArray[i] > 1)
{
foundAny = true;
cout << static_cast<char>(i) << " : " << asciiArray[i] << endl;
}
}
if (!foundAny)
cout << "No repeated characters were found.\n";
}
void findrepeats(字符串常量(&in)
{
int-asciiArray[256];
char ch;
int charconv;
bool-foundAny=false;
对于(inti=0;i<256;i++)asciiArray[i]=0;
for(无符号整数i=0;i1)
{
foundAny=true;
您必须在代码中进行以下更改吗
更改要更新用于比较的引用数组的循环体,并按如下方式设置所有值:
//your code
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
在上述代码中,asciiArray[charconv]
的值不会改变,因为它是增量后的asciiArray[charconv]++;
,将其更改为增量前的++asciiArray[charconv];
或写入asciiArray[charconv]=asciiArray[charconv]+1;
您还可以这样更改循环,更简化:
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
那么你的情况应该是这样的
if((asciiArray[ch] > 1) && (ch!=alreadyprinted) && (found == alreadyprintedcharacters.end()))
我不太明白为什么您需要所有这些代码(假设您声明不能使用std::map
)
您声明了一个256的数组,并将每个项设置为0,这是正常的:
for (int i = 0; i < 256; i++)
asciiArray[i] = 0;
这是对字符串进行预处理的整个循环。然后,在这之后需要一个循环来打印结果
至于打印,仅当存在重复项时才检查数组,您只需检查dup
值即可知道这一点。如果字符i
处的数组值大于1,则打印该字符的信息,如果不是,则跳到下一个
我不会显示最后一步的代码,因为这是家庭作业。上周刚刚遇到类似的问题,下面是我所做的,可能不是最好的解决方案,但它确实很有效
string str("aer08%&#&%$$gfdslh6FAKSFH");
vector<char> check;
vector<int> counter;
//subscript is the bridge between charcheck and count. counter[sbuscript] store the times that check[subscript] appeared
int subscript = 0;
bool charisincheck = false;
for (const auto cstr : str) //read every char in string
{
subscript = 0;
charisincheck = false;
for (const auto ccheck : check) // read every element in charcheck
{
if (cstr == ccheck)//check if the char get from the string had already existed in charcheck
{
charisincheck = true; //if exist, break the for loop
break;
}
subscript++;
}
if (charisincheck == true) //if the char in string, then the count +1
{
counter[subscript] += 1;
}
else //if not, add the new char to check, and also add a counter for this new char
{
check.push_back(cstr);
counter.push_back(1);
}
}
for (decltype(counter.size()) i = 0; i != counter.size(); i++)
{
cout << check[i] << ":" << counter[i] << endl;
}met
string str(“aer08%&%$$gfdslh6FAKSFH”);
向量检查;
向量计数器;
//下标是charcheck和count之间的桥梁。计数器[sbuscript]存储check[subscript]出现的时间
int下标=0;
bool charisincheck=假;
for(const auto cstr:str)//读取字符串中的每个字符
{
下标=0;
charisincheck=假;
for(const auto ccheck:check)//读取charcheck中的每个元素
{
if(cstr==ccheck)//检查从字符串获取的字符是否已经存在于charcheck中
{
charisincheck=true;//如果存在,则断开for循环
打破
}
下标++;
}
if(charisincheck==true)//如果字符在字符串中,则计数+1
{
计数器[下标]+=1;
}
else//如果没有,则添加要检查的新字符,并为此新字符添加计数器
{
检查。推回(cstr);
计数器。推回(1);
}
}
for(decltype(counter.size())i=0;i!=counter.size();i++)
{
coutimport java.util.*;
二硅酸盐类{
公共静态void main(字符串arg[]){
扫描仪sc=新的扫描仪(System.in);
字符串str=sc.nextLine();
int d[]=新int[256];
整数计数=0;
对于(int i=0;i <代码> //这里C++中字符串中的重复字符的简单代码)
#包括
#包括
#包括
void main(){
clrsc();
char-str[100];
cin>>str;
int d[256];
整数计数=0;
对于(int k=0;kd),字母序列“eof”是否出现在代码中的任何位置?不,eof表示文件结束,在这里我没有使用文件。它需要用户输入。@Hikari您知道这是一个使用std::map
?的4行或5行程序。@Paul
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
bool dup = false;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
if ( asciiArray[ch] > 1 )
dup = true;
}
string str("aer08%&#&%$$gfdslh6FAKSFH");
vector<char> check;
vector<int> counter;
//subscript is the bridge between charcheck and count. counter[sbuscript] store the times that check[subscript] appeared
int subscript = 0;
bool charisincheck = false;
for (const auto cstr : str) //read every char in string
{
subscript = 0;
charisincheck = false;
for (const auto ccheck : check) // read every element in charcheck
{
if (cstr == ccheck)//check if the char get from the string had already existed in charcheck
{
charisincheck = true; //if exist, break the for loop
break;
}
subscript++;
}
if (charisincheck == true) //if the char in string, then the count +1
{
counter[subscript] += 1;
}
else //if not, add the new char to check, and also add a counter for this new char
{
check.push_back(cstr);
counter.push_back(1);
}
}
for (decltype(counter.size()) i = 0; i != counter.size(); i++)
{
cout << check[i] << ":" << counter[i] << endl;
}met
import java.util.*;
class dublicate{
public static void main(String arg[]){
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
int d[]=new int[256];
int count=0;
for(int i=0;i<256;i++){
d[i]=0;
}
for(int i=0;i<str.length();i++){
if(d[str.charAt(i)]==0)
for(int j=i+1;j<str.length();j++){
if(str.charAt(i)==str.charAt(j)){
d[str.charAt(i)]++;
}
}
}
for(char i=0;i<256;i++){
if(d[i]>0)
System.out.println(i+" :="+(d[i]+1));
}
}
}
//here simple code for duplicate characters in a string in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char str[100];
cin>>str;
int d[256];
int count=0;
for(int k=0;k<256;k++){
d[k]=0;
}
for(int i=0;i<strlen(str);i++){
if(d[str[i]]==0)
for(int j=i+1;j<strlen(str);j++){
if(str[i]==str[j]){
d[str[i]]++;
}
}
}
for(int c=0;c<256;c++){
if(d[c]>0)
cout<<(char)c<<" :="<<(d[c]+1)<<"\n";
}
getch();
}