C++ C+中代码输出的问题+;
我的代码有问题 我的代码是:C++ C+中代码输出的问题+;,c++,arrays,count,counter,C++,Arrays,Count,Counter,我的代码有问题 我的代码是: #include <iostream> #include <string> #include <math.h> using namespace std; int min(int A[], int s) { int x = A[0]; for (int i = 0; i<s; i++) if (A[i]<x) x = A[i]; return x; }
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int min(int A[], int s)
{
int x = A[0];
for (int i = 0; i<s; i++)
if (A[i]<x)
x = A[i];
return x;
}
int max(int A[], int s)
{
int x = A[0];
for (int i = 0; i<s; i++)
if (A[i]>x)
x = A[i];
return x;
}
int main()
{
int Array[10] = { 15,20,8,0,17,14,2,12,10,5 };
while (1)
{
string UserInput;
cin >> UserInput;
if (UserInput == "Minimun")
{
int Minimum = min(Array, 10);
}
if (UserInput == "Maximum")
{
int Maximum = max(Array, 10);
}
if (UserInput == "Dropped ones")
{
int count = min(Array, 10) + 1;
for (int i = min(Array, 10); i<max(Array, 10) - 1; i++)
cout << count++ << "\n";
}
}
return 0;
}
为什么我的代码不打印此输出
请帮帮我,我不知道这段代码中哪里有错误的句子。
提前谢谢
另一次尝试,但给出了一个“cbegin”和“cend”错误:
#包括
#包括
#包括
使用名称空间std;
int main()
{
int数组[]={15,20,8,0,17,14,2,12,10,5};
自动最小值=*最小元素(cbegin(数组)、cend(数组));
自动最大值=*最大元素(cbegin(数组)、cend(数组));
如果我正确理解了你的帖子,我就可以了。根据你的要求,我把代码从使用向量改为使用c数组。我拿出了用户输入的东西,给你一个小样本看看
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int Array[] = { 15, 20, 8, 0, 17, 14, 2, 12, 10, 5 };
// part of the xutility header
// better to use cbegin( Array );
auto begin = &Array[ 0 ];
// better to use cend( Array );
auto end = begin + sizeof( Array ) / sizeof( Array[ 0 ] );
auto Minimum = *min_element( begin, end );
auto Maximum = *max_element( begin, end );
cout << "Min: " << Minimum << '\n';
cout << "Max: " << Maximum << '\n';
for( auto i = 1; i <= 20; ++i ) {
if( find( begin, end, i ) == end ) {
cout << i << "\n";
}
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int数组[]={15,20,8,0,17,14,2,12,10,5};
//xutility头的一部分
//最好使用cbegin(数组);
自动开始=&数组[0];
//更好地使用cend(阵列);
自动结束=开始+sizeof(数组)/sizeof(数组[0]);
自动最小值=*最小元素(开始、结束);
自动最大值=*最大元素(开始、结束);
cout它不起作用,因为您的cin只得到第一个单词“Dropped”,而不是“Dropped one”。如果您将其设置为1个单词,则似乎效果很好:
如果需要多个单词,请使用getline
std::string UserInput;
std::getline (std::cin, UserInput);
也可以考虑使用STL库,如使用C++中的那些。因为你正在使用C++,你确实应该使用<代码> STD::vector < /C> >,代码> STD::MixEngult,<代码> STD::Max元素> /Calp>等等。请用数组来制作这个代码。谢谢。{15,20,8,0,17,14,2,12,10,5};'to'int Array[]={15,20,8,0,17,14,2,12,10,5};'。全局开始和结束类型函数也可以处理数组。请问,“cbegin”和“cend”是什么?我不明白。请进一步解释。你能用你的代码现在看起来的样子来编辑你的问题吗?我猜你没有#包括begin(),end()类型函数基本上给出容器的开始或容器的结束。因此begin(Array)返回&Array[0],cbegin(Array)返回const&Array[0].我试过这个,但它不能使输出像我想要的那样。你想要它是什么输出?如果它是{1,2..19},似乎工作正常。请检查我提供的测试链接:。我想制作一个计数器,用于打印0到20之间的值,数组中的值除外。这意味着输出应为:1 3 4 6 7 9 11 15 16 18 19
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int Array[] = { 15, 20, 8, 0, 17, 14, 2, 12, 10, 5 };
// part of the xutility header
// better to use cbegin( Array );
auto begin = &Array[ 0 ];
// better to use cend( Array );
auto end = begin + sizeof( Array ) / sizeof( Array[ 0 ] );
auto Minimum = *min_element( begin, end );
auto Maximum = *max_element( begin, end );
cout << "Min: " << Minimum << '\n';
cout << "Max: " << Maximum << '\n';
for( auto i = 1; i <= 20; ++i ) {
if( find( begin, end, i ) == end ) {
cout << i << "\n";
}
}
return 0;
}
std::string UserInput;
std::getline (std::cin, UserInput);