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 7805131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:07:27+00:00 2026-06-02T02:07:27+00:00

i am using an empty vector of structs. now, when i am entering data

  • 0

i am using an empty vector of structs.

now, when i am entering data to one of the struct members, does it change the size of the vector?

if yes, how should i initialize the iterator?
i have a runtime error, and my guess is that my iterator is invalid.

some relevant code:

   struct wordstype
{
    string word;
    int counter_same;
    int counter_contained;
    int counter_same1;
};
 std::vector<wordstype>::iterator iv=vec1.begin();
string temp_str;
string::iterator is=str1.begin();

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();
    }
}
  • 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-02T02:07:29+00:00Added an answer on June 2, 2026 at 2:07 am

    Changing values of the struct members doesn’t affect the size of the vector. You get a run-time error, because you’re trying to access the first element of an empty vector.

    Try this instead:

    wordstype wt;       // create a new struct
    wt.word = temp_str; // set its elements as desired
    vec1.push_back(wt); // insert the new struct into the empty vector
    

    Alternatively you can declare the vector as

    vector<wordstype> vec1(1);
    

    which would initialize it with size 1. Then your current code will work (somewhat).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should I prepare a empty DB inadvance while using the SQliteOpenHelper to create db
Quick one. I'm using mod rewrite and have most replacements in place: empty space
I have some trouble with removing a empty vector in a vector using the
using namespace std; vector<vector<T> > vec_collection(3); vec_collection[0]=vector<T>(12); vec_collection[1]=vector<T>(3); vec_collection[2]=vector<T>(14); Suppose I have an empty
I want to check if a table body ( tbody ) is empty using
I am using Capistrano to bootstrap an empty Ubuntu VM. I have a base
How do I append an empty line in a text file using the command
I'm using regEx and replace method to replace an empty space with a dash
How to show in the WPF Listview using the GridView an empty text (like
I need to check if a database is totally empty (no tables) using an

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.