【问题标题】:How to use Arrays, with Strings如何使用数组和字符串
【发布时间】:2015-06-26 22:16:33
【问题描述】:

我是一个 C++ 程序员,还在窝里,还没有找到我的翅膀。我正在编写一个日历程序,我发现 C++ 不支持字符串类型。如何制作一个可以存储字符串的数组?

我想过创建一个枚举数据类型,作为数组类型。虽然它可以工作,但对于我的日历来说,如果说我正在创建一个班级学生姓名的数据库,它将无法工作。

http://prntscr.com/7m074w我知道了; “错误,'string' 没有命名类型。”

【问题讨论】:

  • string 类到现在已经是 C++ 的一部分了大约 20 年,并且从一开始就是 char 的数组。也许你应该投资一本书?
  • 书?如果 Google 对“C++ 字符串”的第一次点击不是 cppreference.com 的 string 页面,那是因为它是 Cplusplus.com 的
  • and I discovered, that C++ does not support a string type. 嗯? coliru.stacked-crooked.com/a/064380233cd870e0
  • 我还没有开始使用类。我仍在进行程序编程。我试图为字符串prntscr.com/7m074w 创建一个类型。我了解 char 类型的数组。我正在考虑为名称创建一个数据库。使用多个 char 数组,因为这不是很健壮。
  • 我正在使用一本书:“progcpp.pdf” 但是,我还没有接触到数组,我正在尝试按顺序阅读这本书。我添加的链接显示了我得到的错误。我试图为日历创建一个字符串数组。一个维度表示年份,一个维度表示月份,一个维度表示工作日,一个维度表示每个工作日。

标签: c++ arrays


【解决方案1】:
that C++ does not support a string type.

信息错误,可以如下创建字符数组

   char array[length]; 
//Where length should be a constant integer

否则你可以依赖标准模板库容器,std::string

如果你有 C++11 编译器,你可以依赖 std::array

【讨论】:

    【解决方案2】:

    C++ 标准库包含一个字符串类型std::string。见http://en.cppreference.com/w/cpp/string/basic_string

    标准库还提供了固定大小的数组类型std::array。见http://en.cppreference.com/w/cpp/container/array

    但您可能还想了解动态大小的数组类型std::vector。见http://en.cppreference.com/w/cpp/container/vector

    该语言还包括对 c 字符串和 c 数组的传统支持,您可以在一本好的 C++ 或 C 书籍中找到这些支持。见The Definitive C++ Book Guide and List

    如何使用字符串数组/向量的示例:

    #include <string>
    #include <array>
    #include <vector>
    #include <iostream>
    
    int main() {
        std::array<std::string, 3> stringarray;
        stringarray[0] = "hello";
        stringarray[1] = "world";
        // stringarray[2] contains an empty string.
    
        for (size_t i = 0; i < stringarray.size(); ++i) {
            std::cout << "stringarray[" << i << "] = " << stringarray[i] << "\n";
        }
    
        // Using a vector, which has a variable size.
        std::vector<std::string> stringvec;
    
        stringvec.push_back("world");
        stringvec.insert(stringvec.begin(), "hello");
        stringvec.push_back("greetings");
        stringvec.push_back("little bird");
        std::cout << "size " << stringvec.size()
                  << "capacity " << stringvec.capacity()
                  << "empty? " << (stringvec.empty() ? "yes" : "no")
                  << "\n";
    
        // remove the last element
        stringvec.pop_back();
        std::cout << "size " << stringvec.size()
                  << "capacity " << stringvec.capacity()
                  << "empty? " << (stringvec.empty() ? "yes" : "no")
                  << "\n";
    
        std::cout << "stringvec: ";
        for (auto& str : stringvec) {
            std::cout << "'" << str << "' ";
        }
        std::cout << "\n";
    
        // iterators and string concatenation
        std::string greeting = "";
        for (auto it = stringvec.begin(); it != stringvec.end(); ++it) {
            if (!greeting.empty()) // add a space between words
                greeting += ' ';
            greeting += *it;
        }
        std::cout << "stringvec combined :- " << greeting << "\n";
    }
    

    现场演示:http://ideone.com/LWYevW

    【讨论】:

      【解决方案3】:

      您可以通过char name[length]; 创建一个字符数组。 C++ 也有一个数据类型字符串。您可以创建一个字符串数组并存储您想要的值。 here.

      所以

      1. 使用字符数组
      2. 使用字符串数据类型

      例如-

      #include <iostream>
      #include <string>
      int main ()
      {
        //To Create a String
        std::string s0 ("Initial string");
        return 0;
      }
      

      【讨论】:

      • 添加#include &lt;string&gt; 告诉编译器什么是字符串类型。希望这能解决您的问题。
      【解决方案4】:

      C++ 确实有一个字符串类型:string from #include &lt;string&gt; 如果不想使用,也可以使用char* name = "YourTextHere..." 或 `char[length+1] name = "YourTextHere"

      【讨论】:

      • 错误的包含文件。 std::string 的包含是 #include &lt;string&gt;,请注意缺少的“c”。 cstring 头文件用于支持 C 风格的字符数组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2012-02-14
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多