在循环语句内给向量添加元素后,循环外无法访问向量元素

这个程序是统计输入文本中 (除去特定单词外)各单词出现的频率并按照降序处理

为什么这个代码在while循环后无法输出向量中的元素呢?另外如果我要把这个编成一个类的话,那所有的变量是不是应该都放到对象的属性中呢?

代码如下:

# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
 #include <iomanip>
using namespace std;

int main()
{
       int  pos;
     string word;//缓存字符串
     string sep(" ,.?!:\"\t\n()");
     vector<string> word_set;//确认不是stopword词汇后的单词集放进去
     vector<int> word_num;//单词集对应的次数
    vector<string> stop_word;//应删去的词
    typedef vector<string>::size_type vect_sz;
    vect_sz size = 0;//向量的size
    bool flag1,flag2;

    stop_word.push_back("to");
    stop_word.push_back("in");
    stop_word.push_back("will");
    stop_word.push_back("of");
    stop_word.push_back("be");
    stop_word.push_back("and");
    stop_word.push_back("a");
    stop_word.push_back("the");

   cout<<"enter the text"<<endl;
    while(cin>>word) 
  {
    // 除去每一个word多余的符号
       pos = word.find_first_of(sep);
     if ( pos == word.size()-1) //符号出现在尾部
     {
          string tmp(word.substr(0, pos));
            word = tmp;
     }
     else if (pos == 0)//符号出现在头部
     {
            string::size_type pos2 = word.find_first_not_of(sep, 1);
            pos = word.find_first_of(sep, pos2+1);
            string tmp(word.substr(pos2, pos-1));
            word = tmp;
       }
       // 除去stop word
       flag1=false;
      for (int i = 0; i < stop_word.size(); i++)
      {
        if (word==stop_word[i])
        {
          flag1=true;
        }
      }
      if (flag1)
      {
        continue;
      }
//判断单词集是否存在单词或者与word相同的单词
    size = word_set.size();
    flag2 = true;
      for (int i=0; i!=size; ++i) 
            if (word_set[i] == word) {
               ++word_num[i];
                flag2 = false;
                break;
           }
 //存入单词集并在次数向量中对应+1
       if (flag2)
       {
           word_set.push_back(word);
           word_num.push_back(1);
       }
    }
    //给单词次数进行降序处理,同时改变单词顺序
     for (int i = 0; i < word_set.size()-1; i++)
  {
    int currentmax = word_num[i];
    int currentmaxindex = i;
    string temps_storage;
    for (int j=i+1; j < word_set.size(); j++)
    {
        if (word_num[j]>currentmax)
        {
            currentmax = word_num[j];
            temps_storage = word_set[i];
            currentmaxindex = j;
        }
    }
    if (currentmaxindex!=i)
    {
        word_num[currentmaxindex] = word_num[i];
        word_num[i]=currentmax;
        word_set[currentmaxindex] = word_set[i];
        word_set[i] = temps_storage;       
    }
  } 
    // 输出单词以及对应的频率
     for (int i = 0; i < word_set.size(); i++)
       {
          cout<<word_set[i]<<" "<<word_num[i]<<endl;
       }
     
     
    return 0;
}

你需要输入Ctrl+Z回车才结束while循环

img