简体   繁体   English

C ++:问题向量STL

[英]C++:questions vector STL

I did getting divisors by calling functions named getDivisors and return their values formatted by container vector<int> . 我确实通过调用名为getDivisors函数来获得除数,并返回以容器vector<int>格式化的值。

Since I am a new to C++ container, I tried to print my divisor integers by for loops using an iterator. 由于我是C++容器的新手,因此我尝试使用迭代器通过for循环打印除数整数。 However, in my opinions, it seems too complex. 但是,在我看来,这似乎太复杂了。 Is there any easy way to show stored integers in vector STL ? 有什么简单的方法可以显示矢量STL存储的整数?

And I don't get it why the iterator variable it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I did 而且我不明白为什么迭代器变量it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I did it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I did it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I did it not it` it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I didnot it is pointer type? Could you explain it more about it? I was confused that the compilers show the error message when I did `

Below are my simple codes. 以下是我的简单代码。

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

vector<int> getDivisors(int input)
{
    vector<int> divisors;
    divisors.push_back(1); //default
    for (int i = 2; i < input; i++){
        if (input%i == 0){
            divisors.push_back(i);
        }
    }

    return divisors;
}

void solve()
{
    int input;
    cin >> input;
    vector<int> divisors = getDivisors(input);

    for (vector<int>::iterator it = divisors.begin(); it != divisors.end(); ++it)
    {
        cout << *it << endl;
    }
}


int main(void)
{
    int num;
    cin >> num;
    for (int i = 0; i < num; i++){
        solve();
    }
    return 0;
}

You haven't mentioned which compiler you are using, but in C++11 conforming compilers you can use auto and the Ranged-based for loop 您没有提到要使用哪个编译器,但是在符合C ++ 11的编译器中,您可以使用auto基于Ranged的for循环

for (auto i : divisors)
{
    cout << i << endl;
}

i here is not an iterator, it's the container template type which in your case is an int i这里不是迭代器,这是容器模板类型,在您的情况下是int

A pointer is a kind of iterator specifically a random access iterator . 指针是一种迭代器,特别是随机访问迭代器 Iterators are designed as abstractions of pointers with operators like * , -> , ++ , -- , etc. for accessing containers . 迭代器被设计为指针的抽象,并带有*->++--等操作符来访问容器

For C++ programmers, cplusplus.com is your friend. 对于C ++程序员, cplusplus.com是您的朋友。

It is not a pointer, it's an iterator. 它不是指针,而是迭代器。 It overrides operator * to provide pointer-like behavior. 它覆盖operator *以提供类似指针的行为。 You can read more about C++ STL to understand that. 您可以阅读有关C ++ STL的更多信息来了解这一点。

If you are using C++11 or later, use this: 如果您使用的是C ++ 11或更高版本,请使用以下命令:

for (auto x : divisors) cout << x << endl;

Iterators are convenient abstractions which help in accessing a container. 迭代器是方便的抽象,有助于访问容器。 They are not pointers. 它们不是指针。

One of the things you've got to be careful about, is that an iterator can get invalidated if the container associated with it changes substantially 您必须要注意的一件事是,如果与迭代器相关联的容器发生重大变化,则迭代器可能会失效

Get a good book on STL and get the fundamentals right before proceeding. 在继续之前,请先获得一本有关STL的好书,并掌握正确的基础知识。 Here's a primer but it can only do so much. 这是入门,但只能做很多事情。
http://www.cprogramming.com/tutorial/stl/iterators.html http://www.cprogramming.com/tutorial/stl/iterators.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM