QgsVectorLayer* layer = new QgsVectorLayer("/path/to/layer.shp", "layer_name", "ogr"); int fieldIndex = layer->fieldNameIndex("field_name");
QgsFeatureIterator it = layer->getFeatures(QgsFeatureRequest().setFilterExpression("\"field_name\" = 'value'")); while (it.hasNext()) { QgsFeature feature = it.next(); // do something with the feature }This code uses the field index retrieved in Example 1 to query the layer and retrieve features that match a certain value in the "field_name" field. The QgsFeatureIterator class is used to iterate over the features returned by the query. The package library for QgsVectorLayer and fieldNameIndex is the QGIS C++ API.