0

好的,所以我现在正在尝试编译一些东西,而且我是 C++ 的新手,所以可能是代码本身导致了错误,但是 Eclipse 向我展示的代码本身没有出现红色标记。

这是错误的内容

c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:128:7:错误:分配只读引用'__a'

c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:129:7:错误:分配只读引用'__b'

关于我需要做什么的任何想法?在 Win7 上,使用 Eclipse Juno for C++ 和 MingwCC

这是我正在编译的内容,我添加的唯一新内容是有人告诉我用于排列程序的“交换”内容。

更新排列.cc

 #include <iostream>   // for cout
#include <cstdio>     // for printf()
#include <sstream>    // for stringstream
#include <stdio.h>
#include <string.h>
#include "Permutation.h"
using namespace std;

Permutation::Permutation() {
    /* nothing needed in the constructor */
}

void Permutation::permute(string str) {

    int low = 0;
    int high = str.length();
        int j;
        if (low == high) {
            cout << str << endl;
        } else {
            for (j = low; j <= high; j++) {
            std::swap(str[low], str[j]);
            permute(str, low + 1, high);
            std::swap(str[low], str[j]);
        }
        }
    }


void Permutation::permute(string str, int low, int high) {
//  int j;
//  if (low == high) {
//      cout << str << endl;
//  } else {
//      for (j = low; j <= high; j++) {
//          std::swap(str[j + low], str[j + j]);
//          permute(str, low + 1, high);
//          std::swap(str[j + low], str[j + j]);
//      }
//  }
}

排列.h

#pragma once
#include <string>
using namespace std;
class Permutation {
    public:
        Permutation();

        void permute (string);
        void permute (string, int, int);
    private:
        /* attemp to solve this problem without adding 
         * any instance variables/data members, but
         * you may add private helper function members
         * as many as you need */
};

主文件

#include "Permutation.h"

int main()
{
    Permutation p;


    p.permute ("Permute");
    p.permute ("--*--", 2, 3);
}
4

2 回答 2

0

想出了如何正确交换它。

int low = 0;
int high = str.length() - 1;
// make sure the string is a permutation and not a partial mix.
if (low == high) {
    cout << str << endl;
} else {
    //Takes each initial letter, then permutes the remaining string. Then moves to next character.
    for (int i = low; i <= high; i++) {
        std::swap(str[low], str[i]);
        permute(str, low + 1, high);
        std::swap(str[low], str[i]);
    }

}
于 2012-12-12T05:57:01.720 回答
0

我用 C++ 重写了您链接到的 C 代码:

// this method should be private or protected because
// str is passed by reference and will be modified !
// if you prefer a free standing function, don't add the
// declaration to the header, this for internal use only
void do_permute(std::string& str, unsigned i, unsigned n) {
    // you COULD pass str by value here, which
    // would remove the need to backtrack.
    // however, it would create a new copy for every
    // iteration which is terrible for performance,
    // especially with long strings.
    if(i==n)
        std::cout << str << '\n';
    else
        for(unsigned j=i; j<=n; ++j) {
            std::swap(str[i],str[j]);
            do_permute(str,i+1,n);
            std::swap(str[i],str[j]); // backtrack (undo swap)
        }
}

// this is the public method;
// pass string by value (copy), to allow do_permute()
// to modify the string.
void permute(std::string str, unsigned i=0, unsigned n=0) {
    if( n >= str.length() )
        return; // prevent out of bounds access
    // if n is 0 (default value) use the string length instead
    do_permute(str, i, n ? n : (str.length()-1) );
}

int main() {
    permute("BAR");
    permute("FO0BAR", 3);    // FOO***
    permute("FO0BAR", 0, 2); // ***BAR
}
于 2012-12-12T06:03:01.310 回答