简体   繁体   English

C++ 程序未进入“for”循环

[英]C++ Program is not entering "for" loop

I am making a program that finds how many anagrams does a line contain for homework and I have a problem.我正在制作一个程序,用于查找一行包含多少个字谜作为家庭作业,但我遇到了问题。 My program is not entering a simple "for" loop.我的程序没有进入一个简单的“for”循环。 I've been reading whole evening and I cannot understand what is the problem.我整晚都在阅读,但我不明白问题出在哪里。 I've put 7 control cout-s to see where exactly does it enter and where not.我已经放置了 7 个控制 cout-s 来查看它到底在哪里进入,在哪里不进入。 Results are - 1, 2, 3 ,7 - not even entering the "for" loop.结果是 - 1, 2, 3 ,7 - 甚至没有进入“for”循环。 What could be the problem?可能是什么问题呢? Here is my code:这是我的代码:

#include<iostream>
#include<string>
#include <sstream>
#include<vector>
#include <algorithm>
using namespace std;
bool isAnagram(string x, string y)
{
    vector<char>v;
    int szX = x.size(), szY = y.size();
    for (int i = 0; i < szX; i++)
    {
        if (!(find(v.begin(), v.end(), x[i]) != v.end()))
        {
            v.push_back(x[i]);
        }
    }
    for (int j = 0; j < szY; j++)
    {
        if (!(find(v.begin(), v.end(), y[j]) != v.end()))
        {
            return false;
        }
    }
    return true;
}

void breakStringToWords(string str, vector<string> w)
{
    istringstream ss(str);
    string word;
    while (ss >> word)
    {
        w.push_back(word);
    } 
}

int main()
{ 
    string x;
    vector<string>v;
    int cnt=0,sz;
    while(getline(cin, x))
    {
        if(x=="")
        {
            return 0;
        }
        cout<<1<<endl;
        breakStringToWords(x, v);
        cout<<2<<endl;
        sz=v.size();
        cout<<3<<endl;
        for(int i=0; i<sz; i++)
        {
            cout<<4<<endl;
            if (isAnagram(v[i - 1], v[i]))
            {
                cout<<5<<endl;
                cnt++;
            }
            cout<<6<<endl;
        }
        cout<<7<<endl;
        cout<<cnt<<endl;
    }
    return 0;
}

Thank you in advance!先感谢您!

Here is one error:这是一个错误:

void breakStringToWords(string str, vector<string> w)

You are passing w by value, meaning that the breakStringToWords function is working with a temporary vector.您正在按值传递w ,这意味着breakStringToWords函数正在使用临时向量。 As soon as that function returns, w no longer exists.一旦该函数返回, w就不再存在。

You should pass by reference, not by value: void breakStringToWords(string str, vector<string>& w) // Note the &您应该通过引用传递,而不是通过值传递: void breakStringToWords(string str, vector<string>& w) // Note the &

It is no different than doing something like this:这与做这样的事情没有什么不同:

#include <iostream>
void foo(int x)
{
  x = 10;
}

int main()
{
  int x = 0;
  foo(x);
  std::cout << x; // x is still 0, not 10
}

Same issue, same solution -- pass by reference.同样的问题,同样的解决方案——通过引用传递。

Re:关于:

It entered the loop and threw another exception它进入循环并抛出另一个异常

when i is 0, you are accessing v[-1] :当 i 为 0 时,您正在访问v[-1]

    for(int i=0; i<sz; i++)
    {
        cout<<4<<endl;
        if (isAnagram(v[i - 1], v[i]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM