如何在C ++中返回向量变量的元素数

时间:2014-07-02 05:23:40

标签: c++ vector

我已经宣布了一个载体。

然后我要求用户输入作者的姓名,然后我将作者的姓名添加到集合中:

vector<string> AuthorsCollection;
.
.
AuthorsCollection.pushback(UserInput);

一切都运作良好,但是当我得到矢量的大小时:

AuthorsCollection.size()

它返回字符数,而我刚刚添加了一个输入字符串。我需要获取向量的元素数量,在我的情况下必须是1,而不是插入字符串的字符数。

以下是代码:

int main()
{
    string AuthorNameByUser;

    SimpleClass SC;
    SecondClass SecondClass;
    SC.SetAuthorName("Jack London");

    cout << "This is our selected Author: " << SC.AuthorName << endl;

    cout << "Number of sold works: " << SC.GetAuthorNumOfSoldWorks() << " works." << endl;

    cout << endl;

    cout << "Please type the name of your favorite author: ";

    getline(cin, AuthorNameByUser);

    /*while (AuthorNameByUser != "final")
    {
        getline(cin, AuthorNameByUser);
    }*/

    SecondClass.AddAuthor(AuthorNameByUser);

    vector<string> AuthorsCollection = SecondClass.GetAuthors();

    int AuthorsQuantity = AuthorNameByUser.size();

    cout << "Thank for your particpiation. You have entered \"" << AuthorsCollection[0] << "\"" << endl;

    cout << endl << AuthorsQuantity << endl;
}

3 个答案:

答案 0 :(得分:2)

  

如何在C ++中返回向量变量的元素数

使用std::vector::size()成员函数:

#include <vector>
#include <string>
#include <iostream>

int main()
{
  std::vector<std::string> v;
  v.push_back("Hello, World!");
  std::cout << v.size() << std::endl;
  v.push_back("foo bar baz");
  std::cout << v.size() << std::endl;
}

输出:

1
2

修改

您混淆的原因是您打印的是string的尺寸,而不是vector<string>

string AuthorNameByUser;
....
int AuthorsQuantity = AuthorNameByUser.size();
....
cout << endl << AuthorsQuantity << endl;

答案 1 :(得分:1)

#include <iostream>
#include <string>
#include <vector>

int main(void)
{
    std::string UserInput;
    std::vector<std::string> AuthorsCollection;

    for (int i = 0;;i++) {
        std::cin >> UserInput;
        AuthorsCollection.push_back(UserInput);
        std::cout << AuthorsCollection.size() << std::endl;
    }
    return 0;
}

答案 2 :(得分:0)

如果您希望论坛上的任何人为您提供帮助,您必须提供更多信息。请确保你下次再这样做。 我不知道这是不是你想要做的 - 我只是猜测这里为什么你不尝试类似的东西?

//This works for me:

// preprocessor includes
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

// simplify calls to the standard library
using namespace std;

// class definition & implementation
class second_class
{
public:
    void add_author( string name )
    {
        authors.push_back( name );
    }

    /**
     * This function returns a copy of the authors member variable.
     */
    vector<string> get_authors() const
    {
        return authors;
    }

private:
    vector<string> authors;
};

/**
 * Program execution begins here.
 * Ignores command line arguments.
 * @return An integer representing success, or failure.
 */
int main( int argc, const char * argv[] )
{
    string author_name;                // hold authors names
    second_class second_class;         // instance of a second_class
    vector<string> authors_collection; //

    cout << "Please type the name of your favorite author: ";
    getline( cin, author_name );

    second_class.add_author( author_name );

    authors_collection = second_class.get_authors();

    cout << "Thank for your particpiation. You have entered \""
         << authors_collection[0] << "\""
         << endl;

    cout << endl << authors_collection.size() << endl;

    return EXIT_SUCCESS;
}