如果我想构建一个非常简单的数组
int myArray[3] = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
我应该用std::array吗?
std::array<int, 3> a = {{1, 2, 3}};
Run Code Online (Sandbox Code Playgroud)
使用std :: array比使用常规的有什么好处?性能更高吗?更容易处理复制/访问?
有没有办法让相等运算符用于比较相同类型的数组?
例如:
int x[4] = {1,2,3,4};
int y[4] = {1,2,3,4};
int z[4] = {1,2,3,5};
if (x == y) cout << "It worked!"
Run Code Online (Sandbox Code Playgroud)
我知道原样,它只是比较指针值 - 但我希望有某种typedef技巧或类似的东西,所以它不需要循环或memcmp调用.
可以编写如下所示的代码.
问题:即使我输入正确的员工ID,它也总是说"无效的员工ID".
请告诉我为什么以及如何正确地做到这一点.
#include <iostream>
#include <iomanip>
using namespace std;
char select, js;
char empid[4];
double bSalary, bonus, tot=0.0;
int main()
{
do
{
cout<<"Employee id: ";
cin>>empid;
if(empid=="M001" || empid=="A004" || empid == "M002") //these are employee ids
{
cout<<"Job Status: ";
cin>>js;
if(js=='P' || js=='C')
{
cout<<"Basic Salary: ";
cin>>bSalary;
if(bSalary>75000 && js=='P')
{
bonus = bSalary*(20.0/100.0);
tot = tot + bonus + bSalary;
}
else if(bSalary>75000 && js=='C')
{
bonus = bSalary*(15.0/100.0);
tot = tot + …Run Code Online (Sandbox Code Playgroud) 为什么打印“Popescu”而不是“Ionescu”,因为“Popescu”>“Ionescu”?
#include <iostream>
using namespace std;
int main(){
char v[3][100] = {"Popescu","Ionescu","Vasilescu"};
if(v[0]<v[1]){
cout << v[0];
}else{
cout << v[1];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试执行与数组比较的函数,如果它们相同则返回true.现在数组很简单,稍后会推进但我仍然坚持使用该testEqual功能.所以这是代码
int n = 5;
int array[5] = {5,10,3,4,7};
bubbleSort(pole,n);
int array2[5] = {3,4,5,7,10};
testEqual( array , array2 , "bubbleSort");
Run Code Online (Sandbox Code Playgroud)
这是testEqual我需要在阵列上重制的功能,但我不知道如何.
bool testEqual(int i1, int i2, const string testName) {
bool myresult = (i1 == i2);
return myresult;
}
Run Code Online (Sandbox Code Playgroud)
像bubbleSort这样的其他功能很好,我只需要重新制作testEqual.