【发布时间】:2020-08-29 12:57:02
【问题描述】:
我有这个 C++ 程序,但它没有在控制台中打印任何内容。 我将字符串的内容复制到 2 个向量中并对它们进行计数递增操作。
#include <iostream>
#include<vector>
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
string s;
cin>>s;
int i,j;
vector<char> a;
vector<char> b;
int count1=0;
int count2=0;
int count3=0;
int t=s.length()/2;
for(i=0;i<t-1;t++)
{
a.push_back(s[i]);
}
for(j=t;j<s.length();j++)
{
b.push_back(s[j]);
}
int e1,e2;
for (auto it1 = a.begin(), it2 = b.begin();
it1 != a.end() && it2 != b.end();
++it1, ++it2)
{
if(int(*it1)>int(*it2))
count1++;
if(int(*it1)<int(*it2))
count2++;
if(int(*it1)==int(*it2))
count3++;
}
cout<<min(min(count1,count2),count3);
}
最后我打印了三个计数中的最小值。
【问题讨论】:
-
你试过调试你的程序吗?另外,
#include<bits/stdc++.h>是不好的做法。 -
您可以(至少暂时)计算中间结果。示例: out
-
请提供minimal reproducible example(您非常接近)。缺少的是输入和预期输出以及实际输出的示例。
-
您尝试在几个地方比较不同符号的整数。您应该养成启用更多编译器警告的习惯,例如。 [警告:不同符号的整数表达式的比较:'int' ....} [-Wsign-compare] ]
标签: c++ string algorithm vector data-structures