简体   繁体   English

如何将两个对象作为一个元素添加到向量?

[英]How do i add two objects as one element to a vector?

I written a code here to understand a problem. 我在这里写了一个代码来了解一个问题。 The program is supposed ask for how many numbers you'd like to store in a vector called vNumbers then proceed to create a vector the length of iNumbers (the number of iterations you've picked). 该程序应该询问您要在称为vNumbers的向量中存储多少个数字,然后继续创建一个长度为iNumbers(选择的迭代次数)的向量。 You will then input your first number which will be pushed back into vNumbers and then ask for how many letters you'd like to put in (for each numbers) that would be stored in vector vLetters. 然后,您将输入您的第一个数字,该数字将被推回到vNumbers中,然后询问(每个数字)要输入多少个字母(将存储在矢量vLetter中)。 After that it is supposed to print out the stuff inside vNumbers and vLetters. 之后,应该打印出vNumbers和vLetters中的内容。 I can get the program to accept 1 letter for 1 number but i can't get it to have multiple letters for 1 number. 我可以让程序接受1个数字的1个字母,但我不能让它具有1个数字的多个字母。 So for example if i do this 所以例如如果我这样做

Please enter how many numbers you'd like to add: 请输入您要添加的数字:

2 2

Please enter #1 请输入#1

100 100

how many letters would you like to add (for number 100) 您要添加多少个字母(对于100个字母)

2 2

Please enter a letter 请输入字母

A 一种

Please enter a letter 请输入字母

B

Please enter #2 请输入#2

200 200

How many letters would you like to add (for number 200) 您要添加多少个字母(200个字母)

1 1

Please enter a letter 请输入字母

C C

I should get this 我应该得到这个

You have entered: 您输入了:

100 AB 100 AB

200 C 200摄氏度

Instead what i get is this and i'm not sure why. 相反,我得到的是这个,我不确定为什么。

100 AB 100 AB

200 ABC 200 ABC

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;



int main()
{
    string sLetters, sLettersB, sLettersC, sLettersA, sNumbers;
    vector<string> vLetters, vNumbers;
    int iLetters, iNumbers;

    cout << "how many NUMBERS would you like to add? ";
    cin >> iNumbers;
    cin.ignore();
    for (int j = 0; j < iNumbers; j++)
    {
        cout << j + 1 << "# Enter number ";
        getline(cin, sNumbers);
        vNumbers.push_back(sNumbers);

        cout << "how many LETTERS would you like to add to " << j + 1 << "#? ";
        cin >> iLetters;
        cin.ignore();
        for (int i = 0; i < iLetters; i++)
        {
            cout << "Enter letter";
            getline(cin, sLettersA);
            sLetters += sLettersA;

        }
        vLetters.push_back(sLetters); // How ever many letters are in sLetters should be added to n-1 index # in vector vLetters
    }
    for (int x = 0; x < vLetters.size(); x++)
    {
        cout << vNumbers[x] << "\t \t" << vLetters[x] << endl;


    }
    return 0;
}

The problem is that sLetters isn't being cleared down between iterations of the "entry" loop (this is why you get "ABC" on the second time through (the 'AB' is a left over from first iteration and remain in sLetters until removed). 问题是在“ entry”循环的两次迭代之间不会清除sLetter(这就是为什么您第二次获得“ ABC”的原因(“ AB”是第一次迭代的剩余部分,并一直保留在sLetter中,直到去除)。

But - as the comments above suggest, there's other issues with structure/relationships. 但是,正如上面的评论所暗示的,结构/关系还有其他问题。

You code has a bug. 您的代码有错误。 See below for comments 见下面的评论

    for (int i = 0; i < iLetters; i++)
    {
        cout << "Enter letter";
        getline(cin, sLettersA);
        sLetters += sLettersA;  // sLetters is defined outside for loop and no space

    }

So you can either define sLetters inside the first for loop or use clear() 因此,您可以在第一个for循环内定义sLetters或使用clear()

    sLetters.clear();
    for (int i = 0; i < iLetters; i++)
    {
        cout << "Enter letter";
        getline(cin, sLettersA);
        sLetters += ' ' + sLettersA;
    }

or delete sLetters from the line 3, and define it later 或从第3行中删除sLetters ,然后稍后对其进行定义

    string sLetters;
    for (int i = 0; i < iLetters; i++)
    {
        cout << "Enter letter";
        getline(cin, sLettersA);
        sLetters += ' ' + sLettersA;  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将元素添加到对象向量并打印它们? - How do I add elements to a vector of objects and print them? 如何创建一个包含对象的向量? - How do I create a vector that holds objects? 如何正确地将对象添加到矢量? 无需调用构造函数两次 - How to correctly add objects to vector? Without calling constructor two times 如何将子类对象添加到其父类(C ++)的向量中? - How do I add child class objects to a vector of their parent class (C++)? 如何有效地将唯一对象从一个矢量复制到另一个矢量(由相同对象的子集组成)? - How do I efficiently copy unique objects from one vector to another (which is made up of a subset of identical objects)? 如何将Derived对象添加到unique_ptr的向量中 - How do you add Derived objects to a vector of unique_ptr 如何将这个向量的元素加在一起? - How do I add the elements of this vector together? 向量包含类对象,每个对象包含3个字符串。 如何找到特定的字符串,然后删除整个元素? - A vector holds class objects, The class object contains 3 strings per object. How do i find the specific string, then delete the entire element? 如何使用向量和对象构造函数初始化对象向量? - How do I initialize a vector of objects using both vector and the objects constructor? 如何在std :: vector中生成对象而没有复制? - How do I generate objects in std::vector and without copy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM