【问题标题】:Copying from random position Within an array to another array从数组中的随机位置复制到另一个数组
【发布时间】:2012-02-09 12:49:52
【问题描述】:

我有一个包含一些值的 char 数组。我想将该数组中的值从某个随机索引复制到某个其他随机索引。我该怎么做?

         #include<iostream.h>
         using namespace std;

         int main()
         {
           char ar[100];
           strcpy(ar,"string is strange");
           cout << ar ;
           return 0;
         }

现在 ar 数组包含“字符串很奇怪”。假设我想创建另一个 char 数组 cp ,我想从 ar 的随机索引位置复制值,例如从 7 到 10 。是否有一些我们可以使用的字符串函数? 我知道我们可以使用strncpy 函数,但它会从起始索引复制到提到的字符数。是否有其他功能或strncpy 的重载版本 这将使我能够执行相同的操作?

【问题讨论】:

  • 家庭作业?如果有请添加标签。
  • 您可能想升级到使用&lt;iostream&gt; 的编译器。 .h 变体是 14 岁。

标签: c++


【解决方案1】:

这样做

strncpy (dest, ar + 7, 2);

一般

strncpy (destination, source + start_index, number_of_chars);
   The strncpy() function is similar, except that at most n bytes of src are copied.  Warning: If there
   is no null byte among the first n bytes of src, the string placed in dest will  not  be  null-termi‐
   nated.

因此你需要手动终止字符串:

dest[nos_of_chars] = '\0';

更新

你可以这样使用:

char *make_substring (char *src, int start, int end)
{
  int nos_of_chars = end - start + 1;
  char *dest;
  if (nos_of_chars < 0)
  {
    return NULL;
  }
  dest = malloc (sizeof (char) * (nos_of_chars + 1));
  dest[nos_of_chars] = '\0';
  strncpy (dest, src + start, nos_of_chars);
  return dest;
}

在使用 C++ 时,不要使用 char 字符串进行处理,而是使用 string 类。

【讨论】:

  • 不是从 7-10 中取出,而是从位置 7 中取出 10 个字符,所以是 7-17。
【解决方案2】:

您的代码是 C++,所以使用 STL - 不要创建固定大小的字符数组,使用 std::string。那有一个方法 substr(pos, n)。

所以你的代码是:

std::string str;
str = "string is not so strange";
cout << str << endl;
std::string small;
small = str.substr(7, 3);
cout << small << endl;

比使用 C api 进行可能不安全的指针运算要容易得多。

【讨论】:

    【解决方案3】:

    为了从string1string1复制从p位置开始的n字符,您可以使用:

    strncpy(string2, string1 + p, n);
    

    如果您正在处理 C++ 字符串 (std::string),那么您可以使用 substr 成员函数。

    std::string string1 = "......";
    std::string string2 = string1.substr(p, n);
    

    【讨论】:

      【解决方案4】:

      C++

      你想达到什么目的? “字符串很奇怪”让我想起了拼写检查 -> 排列

      #include <algorithm>
      #include <string>
      #include <iostream>
      
      int main()
      {
          std::string s = "string is strange";
          std::sort(s.begin(), s.end());
      
          while (std::next_permutation(s.begin(), s.end()))
              std::cout << s << "\n";
      
          return 0;
      }
      

      真正只是交换随机位置:http://ideone.com/IzDAj

      #include <random>
      #include <string>
      #include <iostream>
      
      int main()
      {
          using namespace std;
          mt19937 random;
      
          string s = "string is strange";
          uniform_int_distribution<size_t> gen(0,s.size()-1);
      
          for (int i=0; i<20; i++)
          {
              swap(s[gen(random)], s[gen(random)]);
              cout << s << "\n";
          }
      
          return 0;
      }
      

      【讨论】:

        【解决方案5】:

        您可以通过使用例如获取数组中特定位置的地址&amp;ar[i].

        例如,如果您在 return 之前添加以下行

        cout << &ar[6] << '\n';
        

        它会打印出来

        很奇怪

        【讨论】:

          【解决方案6】:

          你可以使用内存复制功能

          void * memcpy ( void * destination, const void * source, size_t num );
          

          在你的例子中

          memcpy(cp,ar+p,sizeof(char)*n)
          

          【讨论】:

            猜你喜欢
            • 2018-10-04
            • 1970-01-01
            • 2023-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多