简体   繁体   中英

Can We Replace value of vector element using Iterator?

How Iterators actually work in vectors.If we need to replace a data element in vector, can we proceed through iterator or do we need some other kind of implementation to do so?

Following is my code for above Query

#include <bits/stdc++.h>
using namespace std;
vector <string> v;
vector <string> :: iterator it;
int main()
{
    int t,l,i;
    cin >> t;
    for(i=0;i<t;i++){
        string s;
        cin >> s;
        v.push_back(s);
    }
    string s2="replaceable String";
    //Now I want to change some string at index i in vector v to string s2
    for(it=v.begin();it!=v.end();it++){
        cout << *it << endl;
    }
    return 0;
}

Iterators are used to point to slots or locations in vectors.

The iterators of a std::vector follow the concept of Random Interators .

You can assign an iterator to the beginning of a vector.

You can assign an iterator to the result of std::find .

You can increment an iterator.

You can access the element in an array by dereferencing the interator.

What else do you need?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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