如何在遍历一个数组的所有元素的同时,保持在另一个数组的同一元素上?

编程语言 2026-07-09

我写了一个简短的代码,用来列出我的梦想车型,然后让用户输入他们的梦想车型,并比较我们是否有任何相同的梦想车型。我使用一个数组存放我的梦想车型,用户的梦想车型则用一个向量来存放。我想添加一个功能:在查看我的某一辆梦想车的同时遍历用户的全部梦想车,看看是否匹配,如果匹配就输出它。我还想实现能够输入名字中带有空格的汽车(例如911 targa)。我还没想清楚如何实现第一点。我用getline来实现第二点,但效果不对。

#include <iostream>
#include <vector>
using namespace std;


string mycars[19] = {"MX5", "560sec", "560sl", "sl65", "lp500", "f40", "demon", "6*6", "g550 cab", "u5000", "slk500", "sl600", "lc79", "sls", "slr", "valkyrie", "cls", "911 targa", "firebird"};
vector<string> yourcars = {};



int main() {
    int i;
    int f;
    int n;
    string drcar;
    cout<<"\nmy dream cars are: \n";

    for (int i = 0; i<18; i++) {
        cout<<mycars[i]<<"\n";
    }
    cout<<"\nhow many dream cars do you have\n";
    cin>>n;
    n++;
    for (int f = 0; f<n-1; f++ ) {
        string yourcar;
        cout<<"\nenter your dream cars please\n";
        getline(cin, yourcar, ' ');
        cin>>yourcar;
        cout<<"\n";
        yourcars.push_back(yourcar);
    }
    if (i>yourcars.size()) {
        for (int i = 0; i<19; i++) {
            if (mycars[i]==yourcars.at(i)) {
                cout<<"\nwe both share a dream car! it is: \n"<<mycars[i];
            } else {
                cout<<"\nwe do not share any dream cars(insert broken heart)\n"<<"\n"<<" your dream car at: "<<i+1<<" is: "<<yourcars.at(i)<<"\nmy dream car at: "<<i+1<<" is: "<<mycars[i]<<"\n";
            }
        }
    } else {
        for (int f = 0; f<yourcars.size(); f++) {
            if (yourcars.at(f)==mycars[f]) {
                cout<<"we both share a dream car! it is: "<<yourcars.at(f);
            } else {
                if (f<19) {
                    cout<<"\nwe do not share any dream cars(insert broken heart)\n"<<"\n"<<"your dream car at: "<<f+1<<" is: "<<yourcars.at(f)<<"\nmy dream car at: "<<f+1<<" is: "<<mycars[f]<<"\n";
                }   else {
                    cout<<"\nwoah! you have more dream cars than me\n";
                    cout<<"\nyour dream car at: "<<f+1<<" is: "<<yourcars.at(f)<<"\n";
                }
            }
        }   
    }
    return 0;
}

解决方案

要读取带空格的名称,你可能需要 getline(cin, yourcar);
来替代 getline(cin, yourcar, ' ');,也来替代 cin >> yourcar;

顺便说一句,为了让代码更易读,我在 your_cars 中使用 _
因为名称 yourcaryourcars 非常相似,所以我使用了 new_car

    for(int i = 0; i < n; i++) {
        string new_car;
        cout << "\nenter your dream cars please\n";
        //cin.ignore();  // remove "\n" before `getline`
        getline(cin, new_car);
        //getline(cin, new_car, ' ');
        //cin >> new_car;
        cout << "\n";
        //cout << "adding car: " << new_car << "\n";  //  to check if it get name with spaces
        your_cars.push_back(new_car);
    }

要处理两个数组/向量,你可能需要嵌套的 for-循环。

    for(int my_idx = 0; my_idx < size(my_cars); my_idx++) {
        for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) {
            if(your_cars.at(your_idx) == my_cars[my_idx]) {
                cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n";
            }
        }
    }

当然,你也可以在循环里嵌套 my_cars 循环,放在 your_cars 的循环内

    for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) {
        for(int my_idx = 0; my_idx < size(my_cars); my_idx++) {
            if(your_cars.at(your_idx) == my_cars[my_idx]) {
                cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n";
            }
        }
    }

你也可以在 if 里面使用 break,在找到匹配的车型时跳过循环的余下部分。

要检查某辆车是否不匹配,你可能需要在内部循环之前再定义一个额外的变量——例如 bool matching = false——并在 if 中把它设为 true,离开内部循环后再检查它是否仍然 false

使用第一种嵌套循环版本,你可以检查我的哪些车不是你的梦想(但不能检查你的哪些车不是我的梦想)。

使用第二种嵌套循环版本,你可以检查你的哪些车不是我的梦想(但不能检查我的哪些车不是你的梦想)。

要同时检查两者,可能需要两种版本,或者需要更复杂的代码并附带一个额外的真/假值向量。


用于测试的完整代码:

#include <iostream>
#include <vector>
using namespace std;


string my_cars[] = {
"MX5", "560sec", "560sl", 
"sl65", "lp500", "f40", 
"demon", "6*6", "g550 cab", 
"u5000", "slk500", "sl600", 
"lc79", "sls", "slr", 
"valkyrie", "cls", "911 targa", 
"firebird"
};

vector<string> your_cars = {};

int main() {
    int n;
    string drcar;

    // ------------------------------------

    cout << "\nmy dream cars are: \n";

    for(int i = 0; i < size(my_cars); i++) {
        cout << my_cars[i] << "\n";
    }

    // ------------------------------------

    cout << "\nhow many dream cars do you have\n";
    cin >> n;
    cin.ignore();  // remove "\n" before `getline`

    for(int i = 0; i < n; i++) {
        string new_car;
        cout << "\nenter your dream cars please\n";
        //cin.ignore();  // remove "\n" before `getline`
        getline(cin, new_car);
        //getline(cin, new_car, ' ');
        //cin >> new_car;
        //cout << "\n";
        cout << "adding car: " << new_car << "\n";  //  to check if it get name with spaces
        your_cars.push_back(new_car);
    }

    // ------------------------------------

    for(int my_idx = 0; my_idx < size(my_cars); my_idx++) {
        for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) {
            if(your_cars.at(your_idx) == my_cars[my_idx]) {
                cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n";
                break;
            }
        }
    }

    // ------------------------------------

    for(int my_idx = 0; my_idx < size(my_cars); my_idx++) {
        bool matching = false;
        for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) {
            if(your_cars.at(your_idx) == my_cars[my_idx]) {
                matching = true;
                break;
            }
        }
        if(!matching) {
            cout << "not your dream: " << my_cars[my_idx] << "\n";
        }
    }

    // ------------------------------------

    for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) {
        bool matching = false;
        for(int my_idx = 0; my_idx < size(my_cars); my_idx++) {
            if(your_cars.at(your_idx) == my_cars[my_idx]) {
                matching = true;
                break;
            }
        }
        if(!matching) {
            cout << "not my dream: " << your_cars[your_idx] << "\n";
        }
    }

    // ------------------------------------

    return 0;
}

结果:

my dream cars are:
MX5
560sec
560sl
sl65
lp500
f40
demon
6*6
g550 cab
u5000
slk500
sl600
lc79
sls
slr
valkyrie
cls
911 targa
firebird

how many dream cars do you have
3

enter your dream cars please
fiat uno
adding car: fiat uno

enter your dream cars please
MX3
adding car: MX3

enter your dream cars please
demon
adding car: demon
we both share a dream car! it is: demon
not your dream: MX5
not your dream: 560sec
not your dream: 560sl
not your dream: sl65
not your dream: lp500
not your dream: f40
not your dream: 6*6
not your dream: g550 cab
not your dream: u5000
not your dream: slk500
not your dream: sl600
not your dream: lc79
not your dream: sls
not your dream: slr
not your dream: valkyrie
not your dream: cls
not your dream: 911 targa
not your dream: firebird
not my dream: fiat uno
not my dream: MX3
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章