C ++:附加到矢量字符串

时间:2013-04-04 18:41:02

标签: c++ string vector append strcpy

我正在写一个“猪拉丁”节目;从用户读取输入(名字和姓氏)使输入小写,并根据名称中的内容更改名称。 如果第一个字母(名字和姓氏)都是元音,我们应该在它的末尾添加“方式”。

如果第一个字母是辅音,我们将取第一个字母,将其移到字符串的末尾,并在其末尾添加“ay”。

我的代码在尝试将字符串添加到字符串末尾时给了我错误。它说它不能将字符串转换为字符,我不确定这意味着什么。 它还说我不能使用输出操作数<<对于字符串,即使我之前已经使用过它。

错误发生在“strcpy”和输出名称的最终代码中。

  

37:错误:无法将参数'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >'的{​​{1}}转换为'char*''1'

     

47:错误:无法将参数'char* strcpy(char*, const char*)'的{​​{1}}转换为'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >''char*'

     

54:错误:与'1'

中的'char* strcpy(char*, const char*)'不匹配

我只是需要一些帮助来修复错误并查看我出错的地方。包含完整的代码。

'operator<<'

4 个答案:

答案 0 :(得分:5)

我已经为您的代码注释了一些问题的解释和解决方案的建议。如果您有任何不明白的地方,请发表评论,我会尽力澄清。

#include <iostream>

// You don't need 'vector' for this.
#include <vector>

// You won’t often need the C string header in C++.
#include <cstring>

#include <string>
using namespace std;
int main()
{
    // These variables are unused.
    int q, s;
    char shea[] = "way";
    char gavin_stop_looking_at_ponies[] = "ay";

    // 'first' and 'last' are both names, not collections
    // of names.
    string first;
    string last;
    vector <string> first;
    vector <string> last;

    // 'endl' is unnecessary here; it outputs a newline and
    // flushes the stream, but standard output is usually
    // line-buffered, meaning that newline flushes the
    // stream regardless.
    cout << "Please enter your first name.\n"
    cout << "Please enter your first name." << endl;

    // If you just want to get one name, 'getline' is perfect.
    getline(cin, first);

    // This loop would run 0 times because 'first' is an
    // empty vector.
    for (int i = 0; i < first.size(); i++)
    {
        getline (cin, first[i]);
        string nfirst = first[i];
        while (nfirst[q])
        {
            nfirst[q] = tolower(nfirst[q]);
        }
        first[i] = nfirst;
    }

    // To make a string lowercase, use 'tolower' on each character.
    // Here's one way to do it:
    for (string::size_type i = 0; i < first.size(); ++i)
        first[i] = tolower(first[i]);

    // Here's another, with C++11 enabled:
    for (auto& c : first)
        c = tolower(c);

    cout << "Please enter your last name.\n";
    cout << "Please enter your last name." << endl;

    // Same thing.
    getline(cin, last);
    for (int j = 0; j < last.size(); j++)
    {
        getline (cin, last[j]);
        string nlast = last[j];
        while (nlast[s])
        {
            nlast[s] = tolower(nlast[s]);
        }
        last[j] = nlast;
    }

    // Now 'first' is a string, and 'first[0]' is a 'char'.
    // "a" is a string literal; 'a' is a character literal.
    // You can compare each character individually:
    if (first[0] == 'a' || first[0] == 'e' || first[0] == 'i' || first[0] == 'o' || first[0] == 'u')

    // Or you can say "if the character was found in this
    // set of vowels".
    if (string("aeiou").find(first[0]) != string::npos)

    if ( (first[0] == "a") ||( first [0] == "e") || (first [0] == "i") || (first [0] == "o")     || (first [0] == "u"))
    {
        // This would try to copy "way" into 'first':
        // formerly a vector of string objects, now just a
        // string object. 'strcpy' wants a character buffer,
        // and will overwrite characters in that buffer—
        // probably not what you want:
        //
        // "aaron" => "wayon"
        // 
        strcpy (first, "way");

        // Instead, just append "way":
        first += "way";
    }
    else
    {
        // This says "take the first first character of the
        // string, add the value of that character to a
        // pointer to a buffer containing "ay", then try to
        // copy the resulting pointer past the end of the
        // string. Again, not quite what you intended!
        first[first.size()] = first[0] + "ay";

        // Think of it instead like this: take everything
        // after the first character, add a string consisting
        // of the first character back onto the end, then add
        // "ay" after that.
        first = first.substr(1) + string(1, first[0]) + "ay";
    }

    // Duplicated code! You could move the above logic into
    // a function to avoid this duplication. Then you only
    // have to work on it in one place. :)
    if ( (last[0] == "a") ||( last [0] == "e") || (last [0] == "i") || (last [0] == "o") || (last [0] == "u"))
    {
        strcpy (last, "way");
    }
    else
    {
        last[last.size()] = last[0] + "ay";
    }

    // I need a space between my first and last names!
    cout << first << ' ' << last << '\n';
    cout << first << last << endl;
    return 0;
}

答案 1 :(得分:1)

firstvector<string>,而不是stringvector <<不支持cout。如果您想在std::string中输出每个std::vector,请尝试迭代std::vector并一次输出一个字符。

同样,firstvector<string>,无法隐式转换为char*strcpychar数据的原始块进行操作,first远离strcpyfirst[first.size()] = first[0] + "ay";用于字符串缓冲区的C级操作(即使这样,也很危险)。

first是未定义的行为,因为您正在访问first的一个过去的最后一个元素,这是无效的内存。如果您想将某些内容推到first.push_back( first[0] + "ay" );的后面,请尝试std::string

很可能您对charchar*std::string之间的区别感到困惑。这些是完全不同的东西。 charchar的托管缓冲区。 'a'是一个8位值,通常用于存储char(类型为char*)等文字。 char是指向单个char的指针,通常用作指向紧密打包的std::vector<std::string>非托管缓冲区的开头的指针。

您的char是托管vector缓冲区的托管缓冲区。 char中的每个元素都是一个完整的缓冲区,其中包含一些未知数量的char s,而不是一个{{1}}。

答案 2 :(得分:0)

我同意之前的一些响应者,但我不想进行详细的代码审查,因为它会很大并且可能会忽略这一点。我认为你遇到的问题是对正确使用C ++标准库的潜在根本误解的症状。大多数情况下,您将标准C库字符串实现与C ++“字符串”对象混合。在C中,有一组函数采用字符指针并将它们作为字符串进行操作,而在C ++中有一个字符串对象,它封装了一串字符并在其上提供了一系列操作。在某些情况下,C ++字符串可以在通常存在C char *的地方使用,但在大多数情况下它不能。

即使在C ++ STL(标准模板库)中,你也会让事情变得混乱。根据您的代码,您似乎想要检索用户的名字和姓氏,即2个单独的字符串。但是,您要声明2个集合(在本例中为向量)字符串,而不仅仅是两个单独的字符串本身。程序中的许多其他问题也随之而来。

我想您可能会喜欢此处的信息:http://en.cppreference.com/w/

答案 3 :(得分:0)

我快速尝试为此编写代码只是为了稍微玩一下。这就是我想出的 - 我认为它非常简洁,干净且易于理解:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* simple pig-latin program ... converts a given "first name" and "last name"*/
/* from the user to pig latin translations.  Obviously, the so-called names  */
/* are really just any 2 random words, but that's the theme anyway.          */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


string xlat_to_piglatin(const string source)
{
    string result;

    //
    // translation: if word begins with a vowel, simply append "way" to
    //  it.  Otherwise, move the initial consonant from the beginning to
    //  the end, and append "ay" to it.
    //
    if (source.find_first_of("aeiou") == 0) {
        result = source;
        result.append("way");
    } else {
        result = source.substr(1, source.size());
        result.append(source.substr(0, 1));
        result.append("ay");
    }
    return result;
}

int main()
{
    string  first;
    string  last;

    //
    // get the first and last names from the user on standard in ...
    //
    cout << "Please enter your first name:" << endl;
    cin >> first;
    cout << "Please enter your last name:" << endl;
    cin >> last;

    //
    // convert strings to lower case ...
    //
    transform(first.begin(), first.end(), first.begin(), ptr_fun<int, int>(tolower));
    transform(last.begin(), last.end(), last.begin(), ptr_fun<int, int>(tolower));

    cout << xlat_to_piglatin(first) << " " << xlat_to_piglatin(last) << endl;
    return 0;
}