Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从vector复制字符串不起作用_C++ - Fatal编程技术网

C++ 从vector复制字符串不起作用

C++ 从vector复制字符串不起作用,c++,C++,我正在尝试将一个单词复制到一个文件中,如果该单词不包含元音。这是我的尝试,但不起作用。它将单词复制到文件中,但不排除带有元音的单词。我不知道为什么它会输出它的功能 #include <iostream> #include <string> #include <sstream> #include <cctype> #include <fstream> #include <vector> template <typena

我正在尝试将一个单词复制到一个文件中,如果该单词不包含元音。这是我的尝试,但不起作用。它将单词复制到文件中,但不排除带有元音的单词。我不知道为什么它会输出它的功能

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <fstream>
#include <vector>

template <typename It>
bool has_vowel(It begin, It end)
{
    for (auto it = begin; it != end; ++it)
    {
        char lower = std::tolower(*it);

        if (lower == 'a' || lower == 'e' ||
            lower == 'i' || lower == 'o' || lower == 'u')
            return true;
    }
    return false;
}

int main()
{
    std::fstream in("in.txt");
    std::fstream out("out.txt");
    std::vector<std::string> v;
    std::string str;

    while (in >> str)
    {
        v.push_back(str);
    }

    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if (!has_vowel(it->begin(), it->end()))
            out << *it << " ";
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
模板
布尔有_元音(它开始,它结束)
{
for(自动it=开始;it!=结束;++it)
{
char lower=std::tolower(*it);
如果(较低==“a”|较低==“e”||
下=='i'| |下=='o'| |下=='u')
返回true;
}
返回false;
}
int main()
{
std::fstream in(“in.txt”);
std::fstream out(“out.txt”);
std::向量v;
std::字符串str;
while(在>>str中)
{
v、 推回(str);
}
对于(自动it=v.begin();it!=v.end();+it)
{
如果(!有_元音(it->begin(),it->end())
out——使用
noskipws
---

实际上,这是一个thinko。下面是使用C++11进行的一个最小的返工。我希望您能从中收集一些有用的信息:

#include <iostream>
#include <iterator>
#include <string>
#include <vector>

template <typename It>
bool has_vowel(It begin, It end)
{
    while (begin!=end)
    {
        char lower = std::tolower(static_cast<unsigned char>(*begin));

        if (lower == 'a' || lower == 'e' ||
            lower == 'i' || lower == 'o' || lower == 'u')
            return true;

        ++begin;
    }
    return false;
}

int main()
{
    std::istream_iterator<std::string> f(std::cin), l;

    for (auto& s : std::vector<std::string>(f, l))
    {
        if (!has_vowel(s.begin(), s.end()))
            std::cout << s << " ";
    }
}
#包括
#包括
#包括
#包括
模板
布尔有_元音(它开始,它结束)
{
while(开始!=结束)
{
char lower=std::tolower(静态_cast(*begin));
如果(较低==“a”|较低==“e”||
下=='i'| |下=='o'| |下=='u')
返回true;
++开始;
}
返回false;
}
int main()
{
std::istream_迭代器f(std::cin),l;
用于(自动&s:std::vector(f,l))
{
如果(!有_元音(s.begin(),s.end())

std::cout除非你感到受虐狂,否则几乎可以肯定,使用
find\u first\u of
对元音检查进行编码要容易得多:

struct has_vowel { 
    bool operator()(std::string const &a) { 
        static const std::string vowels("aeiouAEIOU");

        return a.find_first_of(vowels) != std::string::npos;
    }
};
如果要复制某些容器,但排除满足条件的项,通常需要使用
std::remove\u copy\u if
。因为这可以直接与
istream\u迭代器
ostream\u迭代器
配合使用,所以在执行此任务时,不需要将所有单词存储在向量中:

std::remove_copy_if(std::istream_iterator<std::string>(in),
                    std::istream_iterator<std::string>(),
                    std::ostream_iterator<std::string>(out, " "),
                    has_vowel());
std::remove\u copy\u if(std::istream\u迭代器(in),
std::istream_迭代器(),
std::ostream_迭代器(out,“”),
has_元音();
如果您愿意使用C++11,可以在以下条件下使用lambda:

std::remove_copy_if(std::istream_iterator<std::string>(in),
                    std::istream_iterator<std::string>(),
                    std::ostream_iterator<std::string>(out, " "),
                    [](std::string const &s) { 
                        return s.find_first_of("aeiouAEIOU") != std::string::npos;
                    });
std::remove\u copy\u if(std::istream\u迭代器(in),
std::istream_迭代器(),
std::ostream_迭代器(out,“”),
[](std::string const&s){
返回s.find_first_of(“aeiouAEIOU”)!=std::string::npos;
});

您错误地打开了输出文件。
std::fstream
不会丢弃文件的旧内容。请改用
std::oftream

if(!callback(it))
应该是
if(!callback(*it))
,不是吗?对我来说很有用:@H2CO3是的,我输入了一个错误。我得到的out.txt输出是
myllofriendsandfamily
,而不是
my
。我想你忘记了第四个参数,如果
remove\u copy\u,它接受回调。你还忘了
ostream\u迭代器
lolYou在
std::string::npos
后面加一个括号
,这个括号也不属于我。不过我会接受的,谢谢!:@MemyselfandI:这几年我真的需要学习如何打字(或者至少校对).@MemyselfandI Mmm.一定有什么与您发布的代码不同的地方:/很高兴我能让您跟踪
skipws
。作为奖励,请查看我的C++11版本。C++11 rocks!添加了一个避免临时存储的变体(即流式存储)没有Jerry的回答那么简洁/cc@JerryCoffinGood catch。我下意识地注意到了这一点,但我手动清除了文件。我会记住这一点!老实说,我认为这才是真正的问题。
std::remove_copy_if(std::istream_iterator<std::string>(in),
                    std::istream_iterator<std::string>(),
                    std::ostream_iterator<std::string>(out, " "),
                    [](std::string const &s) { 
                        return s.find_first_of("aeiouAEIOU") != std::string::npos;
                    });