c ++:如何将数据插入到结构成员(位于向量中的struct) [英] c++: how to insert data to a struct member (struct located in vector)

查看:164
本文介绍了c ++:如何将数据插入到结构成员(位于向量中的struct)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你在标题中看到的,我正在处理一个结构向量。



其中一个结构成员是 string word
当我试图以这种方式输入数据给这个成员:(* iv).word = temp_str; ,我得到一个运行时错误。

  while(is!= str1.end())
{
if(((* is)!=' - ')& &((* is)!=';')&&((* is)!=',')&是)!$'$''))
{
temp_str。)'='?')&&((* is)! push_back(* is);
++ is;
}
else
{
(* iv).word = temp_str;
++ iv;
str1.erase(is);
temp_str.clear();
}
}

这可能是相关的代码间隔。 >

应该说 - word和temp_str是字符串类型。
iv是向量的迭代器。



在这种情况下,什么是正确的方式输入数据到struct成员?

解决方案

您的迭代器可能无效,否则不应该是将一个字符串分配给另一个字符串的问题。



一个问题是行:

  str1.erase 

这将使失效更改为:

  is = str1.erase(is); 

iv 是什么?您似乎需要添加以下内容:

  while(is!= str1.end()&& iv != something.end())


as you could see in the title, I am working on a vector of structs.

one of the struct members is string word. when i am trying to enter data to this member in this way: (*iv).word=temp_str;, i get a runtime error.

while (is!=str1.end())
{
    if (((*is)!='-')&&((*is)!='.')&&((*is)!=',')&&((*is)!=';')&&((*is)!='?')&&((*is)!='!')&&((*is)!=':'))
    {
        temp_str.push_back(*is);
        ++is;
    }
    else
    {        
        (*iv).word=temp_str;
        ++iv;
        str1.erase(is);
        temp_str.clear();
    }
}

this may be the relevant code interval.

should say- word and temp_str are of string type. iv is an iterator to the vector.

what is the right way to enter data into a struct member in this case?

解决方案

Your iterator is probably invalid, otherwise it shouldn't be a problem assigning one string to another.

One problem is the line:

str1.erase(is);

This will invalidate is, you should probably change it to:

is = str1.erase(is);

What does iv point at? It seems like you would need to add something like:

while (is!=str1.end() && iv != something.end())

as well.

这篇关于c ++:如何将数据插入到结构成员(位于向量中的struct)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆