Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:41:21+00:00 2026-06-18T06:41:21+00:00

See this code. I need to change the value of a specific element in

  • 0

See this code. I need to change the value of a specific element in a 2D string vector using the iterator. I can use for loop with an index to do this. but here what i need is directly use the iterator to refer the element. something like (*ite)[0] = "new name"
Any idea? I added full working code here for your convenience

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;


string convertInt(int number)
{
   stringstream ss;
   ss << number;
   return ss.str();
}

int main()
{
    vector<vector<string>> studentList;
    for(int i=0; i<10; i++){
        vector<string> student;
        student.push_back("name-"+convertInt(i));
        student.push_back(convertInt(i+10));
        studentList.push_back(student);
    }
    vector<string> temp;
    for(vector<vector<string>>::iterator ite = studentList.begin(); ite != studentList.end(); ++ite){
        temp = *ite;
        if(temp[0].compare("name-5")==0){
            cout << "Changeing the name of student 5" << endl;
            // I need to change the studentList[5][0] (original value not the one in temp vector) at here using iterator ite
        }
    }
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:41:22+00:00Added an answer on June 18, 2026 at 6:41 am

    Because temp = *ite makes a copy of vector(student), if you modify on temp, it’s not modified on real studentList, that’s why you need (*ite)[0] = "new name" to change value on real element.

    Using for loop is a bit “ugly“, use std::find_if instead of for loop:

    bool IsFindFifthName(const std::vector<std::string>& student)
    {
       return student[0] == "name-5";
    }
    
     std::vector<std::vector<std::string>>::iterator iter 
      = std::find_if(studentList.begin(), studentList.end(), IsFindFifthName);
    
    if (iter != studentList.end() )
    {
       (*iter)[0] = " new name";       
    }
    

    Or use Lambda if C++11 is available:

    std::vector<std::vector<std::string>>::iterator iter 
      = std::find_if(studentList.begin(), studentList.end(), 
        [](std::vector<std::string>& student){ return student[0] == "name-5"; });
    
     if (iter != studentList.end() )
     {
        (*iter)[0] = " new name";       
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's what I see this code doing: An array is made A loop
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I have this code below. As you can see I am passing two variables
I can't see a clear mistake in this code. Instead of validating my fields,
With this code: void SomeMethod(string str) { string tmpStr = ... Read some value
I want to see if this is possible, i need to change a select
See this code: TicketStoreService fakeTicketStoreService = MockRepository.GenerateMock<TicketStoreService>(); fakeTicketStoreService.Expect(service => service.DoSomething(Arg.Is(new Guid())) .Return(new Guid()); fakeTicketStoreService.DoSomething(Arg.Is(new
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
i'm playing with MEF and in the example i see this code ( i
In facebook tutorials I see this sample code: @Override public void onActivityResult(int requestCode, int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.