【问题标题】:How to move character array certain positions in C++如何在 C++ 中移动字符数组的某些位置
【发布时间】:2014-05-11 12:20:49
【问题描述】:

我有一个字符数组,我想从固定位置开始移动它的某些位置,并为移动的位置数量放置空格。

例如,如果我有数组:

(read A var := 5)

如果我想将它从第一个位置移动一个位置,我想得到:

( read A var := 5)

但是我得到了:

((ead A var := 5)

你可以在下面找到我的源代码:

#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;

char output[100] = "(read A var := 5)";

void move(int spos, int places)
{
    int i = places, j = spos;

    for (int k = 0; k < places; k++) {
        output[j+i] = output[j];
        i++;
        j++;
    }
}

int main(void)
{
    for (i = 0; i < strlen(output); i++)
        cout << output[i];
    cout << endl;

    move(0, 1);
    output[18] = '\0';

    for (i = 0; i < strlen(output); i++)
        cout << output[i];
    cout << endl;

    return 0;
}

【问题讨论】:

  • 如果您使用 C++,为什么不使用std::string
  • 更改 move 循环以向后工作。 (或者使用memmove,它可以正确处理重叠的源和目标。)
  • @OliCharlesworth 因为我需要使用字符。
  • @Jongware 你的意思是 memmove(output+1, output+1, 1); ?虽然它产生相同的字符串而没有任何变化。
  • 类似的东西,是的。 memmove 采用 destinationsourcelength 参数。试试memmove (output+places, output+spos, strlen(output+spos)+1); -- 凭记忆,未经测试。

标签: c++ arrays shift


【解决方案1】:

这是我的移动功能版本,试试看:

#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;

char output[100] = "(read A var := 5)";

/* my version of move ;) */
void move2(int spos, int places)
{
    for(int x = strlen(output); x >=spos; x--)
    {
        output[x+places] = output[x]; /* move character from position x to position x+places */
    }

    for(int x = 0; x < places; x++)
        output[x+spos] = ' ' ; /* now we are adding white spaces to the begining */

}

int main(void)
{
    int i = 0;
    for(i = 0; i < strlen(output); i++)
        cout << output[i];
    cout << endl;

    move2(1, 1);    
    for (i = 0; i < strlen(output); i++)
        cout << output[i];
    cout << endl;

    return 0;
}

【讨论】:

    【解决方案2】:

    试试这个:

    #include "stdafx.h"
    #include <iostream>
    #include <cctype>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    char output[100] = "(read A var := 5)";
    
    void move(int spos, int shift_Len)
    {
        int  j = spos;
        int  i = shift_Len;
        for (int k = strlen(output); k >j; k--) {
            output[k+i]= output[k-1];
        }
        for (int len=0 ; len <=i ; len++ ) {
            output[j+len]=' '; //fill shift position with space char
        }
    }
    
    int main(void)
    {
        int i;
        for (i = 0; i < strlen(output); i++)
            cout << output[i];
        cout << endl;
    
        move(5,10);        //first argument is shift position and second argumnet is number of shift
        output[strlen(output)] = '\0';
    
        for (i = 0; i < strlen(output); i++)
            cout << output[i];
        cout << endl;
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      我实际上已经将这个写到了现在已删除的问题中,这非常相似:

      希望对您有所帮助:Live On Coliru

      #include <boost/fusion/adapted/struct.hpp>
      #include <boost/spirit/include/qi.hpp>
      #include <boost/spirit/include/phoenix.hpp>
      #include <iostream>
      
      using It         = std::string::iterator;
      using tokref     = boost::iterator_range<It>;
      using swappables = std::pair<tokref, tokref>;
      
      namespace qi = boost::spirit::qi;
      
      int main()
      {
          std::string input = "read A var := 5";
      
          It first(input.begin()), last(input.end());
      
          using boost::phoenix::construct;
          using boost::phoenix::push_back;
          using namespace qi;
      
          qi::rule<It, tokref()> token_;
          qi::rule<It, swappables(), qi::space_type> swap_;
      
          token_ = qi::raw[+qi::graph];
          swap_  = (token_ >> token_) [ _val = construct<swappables>(_1, _2) ];
      
          std::vector<swappables> to_swap;
      
          std::cout << "Before: " << input << "\n";
          if (qi::phrase_parse(first, last, 
                      *(
                           (&as_string [token_] [ _pass = ("var" == _1) ] >> swap_)
                         | omit[ token_ ]
                       ), space, to_swap))
          {
              // swap all swappables!
              for (auto& pair : to_swap)
              {
                  std::cout << "Swap '" << pair.first << "' and '" << pair.second << "'\n";
                  auto& a = pair.first;
                  auto& b = pair.second;
      
                  input.replace(a.begin(), b.end(), 
                          std::string(b.begin(), b.end()) +
                          std::string(a.end(), b.begin()) +
                          std::string(a.begin(), a.end()));
              }
          }
          std::cout << "After:  " << input << "\n";
      }
      

      输出:

      read A var := 5
      Swap 'var' and ':='
      read A := var 5
      

      【讨论】:

        猜你喜欢
        • 2020-04-25
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多