如何到达vector <list <int>&gt;?的指定元素

时间:2017-05-09 17:08:40

标签: c++ list vector

我有这样的代码:

vector<list<int> > a(b);

在内部向量中我有几个列表,如下所示:

{2,1},{2,2},{4,5}等。

如何从该向量中的列表中获取指定的元素?

3 个答案:

答案 0 :(得分:1)

列表无法直接访问,因此您需要遍历每个列表。

示例:

std::vector< std::list< int > > a;
a.push_back( {1,2,3} );
a.push_back( {4,5,6} );
a.push_back( {7,8,9} );

// You cannot do this, as list does not have direct access.
// std::cout << a[0][0];

// You must access each element via iterator for each list.  This will print 1...9.
for( auto it = a[0].begin(); it!= a[0].end(); ++it ) std::cout << *it << std::endl;
for( auto it = a[1].begin(); it!= a[1].end(); ++it ) std::cout << *it << std::endl;
for( auto it = a[2].begin(); it!= a[2].end(); ++it ) std::cout << *it << std::endl;

答案 1 :(得分:0)

您可以这样做:

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "com.procibernetica.moca"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
   buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:multidex:1.0.1'

    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'

    compile 'com.google.android.gms:play-services-gcm:9.6.1'
    compile 'com.google.android.gms:play-services-location:9.6.1'
    compile 'com.google.android.gms:play-services-ads:9.6.1'
    compile 'com.google.android.gms:play-services-maps:9.6.1'
    compile 'com.google.android.gms:play-services-auth:9.6.1'

    compile 'com.google.gms:google-services:3.0.0'

    compile 'com.google.firebase:firebase-database:9.6.1'
    compile 'com.google.firebase:firebase-core:9.6.1'
    compile 'com.google.firebase:firebase-auth:9.6.1'


    compile 'com.facebook.android:facebook-android-sdk:[4,5)'

    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'

    compile 'com.github.satyan:sugar:1.5'
    compile 'com.android.support:design:25.3.1'

    compile 'org.msgpack:msgpack:0.6.11'

    compile 'com.theneura:android-sdk:+'

    compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar') {
        transitive = true
    }
    compile project(':moca-sdk-android-1.9.6')
    testCompile 'junit:junit:4.12'

}

首先,我遍历 arr 。然后在每一步我得到i元素(它是 std :: list )并在此列表的第一个元素上创建迭代器。然后我读了这个列表中的所有元素并将它们打印出来。这很容易理解

答案 2 :(得分:0)

要从其中一个列表访问元素,您需要先访问包含所述元素的列表。

例如,要打印大于某个变量的每个元素(假设为2),您可以执行以下操作:

#include <vector>
#include <list>
#include <iostream>

int main (void) {
    std::vector<std::list<int> > vecList;
    int someVariable = 2;        

    for (int i = 1; i < 5; ++i) {
        vecList.push_back(std::list<int> (i, i)); /* fill the vector with 4 lists {1}, {2, 2}, {3, 3, 3} {4, 4, 4, 4} */
    }

    /*this first loop will iterate over the content of the vector, not the content of the lists*/
    for (const auto& ls : vecList) {

        /*this will iterate over the content of each list in the vector*/
        for (auto head = ls.begin(), tail = ls.end(); head != tail; ++head) {
            if (*head > someVariable) { //*head is different from head
                std::cout << *head << std::endl;   
            }
        }
    }
}

N.B:了解std::vectorstd::listiterator