C ++迭代器问题 [英] C++ iterator question

查看:163
本文介绍了C ++迭代器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个面试问题,要求使用iterator来读取 vector< vector< int>> 。我们必须设计必要的界面吗?

I saw an interview question, which was asked to use "iterator" to read vector<vector<int>>. We have to design the necessary interface?

这个问题想要问这个问题很混乱?或者如何回答这类问题。

Quite confusing about does this question want to ask? Or how to answer this kind of question.

我可以想象它打算测试C ++ STL实现和面向对象的设计。

I can imagine that it intends to test C++ STL implementation and objected-oriented design.

推荐答案

矩阵是3 * 4维。如果需要只通过迭代器访问,这应该给你一个想法 -

Matrix is in 3*4 dimension. If needed to access only through iterators, this should give you an idea -

vector< vector<int> > Matrix(3, vector<int>(3,4));

for( vector<vector<int>>::iterator i = Matrix.begin(); i != Matrix.end(); ++i )
{
    for( vector<int>::iterator j = (*i).begin(); j != (*i).end(); ++j )
    {
        cout << *j << "\t" ;
    }
    cout << "\n" ;
}

这篇关于C ++迭代器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆