C++ 如何使用迭代器更新二维字符串向量的元素

C++ 如何使用迭代器更新二维字符串向量的元素,c++,vector,C++,Vector,请参阅此代码。我需要使用迭代器更改2D字符串向量中特定元素的值。我可以使用带有索引的for循环来实现这一点。但这里我需要的是直接使用迭代器来引用元素。类似于(*ite)[0]=“新名称” 有什么想法吗?为了您的方便,我在这里添加了完整的工作代码 #include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; stri

请参阅此代码。我需要使用迭代器更改2D字符串向量中特定元素的值。我可以使用带有索引的for循环来实现这一点。但这里我需要的是直接使用迭代器来引用元素。类似于
(*ite)[0]=“新名称”
有什么想法吗?为了您的方便,我在这里添加了完整的工作代码

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串转换整数(整数编号)
{
细流ss;

ss因为
temp=*ite
复制了vector(student),如果在
temp
上修改,它在real
studentList
上不会被修改,这就是为什么需要
(*ite)[0]=“new name”
来更改real元素的值

使用for循环有点“丑陋”,请使用而不是for循环:

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";       
}
bool isfindfithname(const std::vector&student)
{
返回学生[0]=“姓名-5”;
}
std::vector::iterator iter
=std::find_if(studentList.begin()、studentList.end()、isfindfithname);
if(iter!=studentList.end())
{
(*iter)[0]=“新名称”;
}
如果C++11可用,则使用Lambda:

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";       
 }
std::vector::iter
=std::find_if(studentList.begin(),studentList.end(),
[](std::vector&student){返回学生[0]=“name-5”;});
if(iter!=studentList.end())
{
(*iter)[0]=“新名称”;
}

在案例中使用STL算法转换可能是一个不错的选择。该算法内部使用迭代器。示例:

typedef std::vector<std::string> VectorOfString;
void DisplayStudent(const std::vector<VectorOfString>& StudentList)
{
    std::for_each(StudentList.cbegin(), StudentList.cend(), 
        [](const VectorOfString& vectorElement)
    {
        std::for_each(vectorElement.cbegin(), vectorElement.cend(), 
            [](const std::string& value)
        {
            std::cout << value << endl;
        });     
    });
}

std::vector<VectorOfString> StudentList;

std::string data1 = "One";
std::string data2 = "Two";

VectorOfString data(2);
data.push_back(data1);
data.push_back(data2);

StudentList.push_back(data);



DisplayStudent(StudentList);

std::for_each(std::begin(StudentList), std::end(StudentList), 
    [](VectorOfString& vectorElement)
{
    std::transform(std::begin(vectorElement), std::end(vectorElement), std::begin(vectorElement),
        [](std::string& value)-> std::string
    {
        if(value.compare("One") == 0)
            return "OneOne";
        else
            return value;
    });     
});

DisplayStudent(StudentList);
typedef std::VectorOfString;
void DisplayStudent(const std::vector和StudentList)
{
std::for_each(StudentList.cbegin(),StudentList.cend(),
[](常量向量字符串和向量元素)
{
std::for_each(vectoreElement.cbegin(),vectoreElement.cend(),
[](常量标准::字符串和值)
{

顺便说一下,如果您使用C++11,
convertInt
可以替换为
std::to_string
。另外,使用
std::string
,您不必使用
compare()
。您可以使用
==
@chris安全地检查等式谢谢您的指导。非常感谢您的解决方案。但为什么您会说第一个解决方案有点难看(这不符合最佳实践吗?)?在您的第二个解决方案中,是否有任何方法可以在该位置定义
IsFindFithName
。否则,它需要声明为一个新函数。这是一个额外的负担。是的,这就是为什么ppl喜欢lumbda。学习所有STL算法也很好,它们使代码更清晰、更高效。