从多个字段生成排列(不使用Python)

时间:2015-04-29 01:57:04

标签: permutation

我正在尝试简单地使用常规Unix函数(我理解),而不是Python(我认为itertools可能让我在那里?),我不明白。所以:几个小的输入列表(用逗号分隔),需要用来生成所有可能的排列。例如:

清单1 :(我,你,我们)

列表2 :(想吃,想买,想偷)

清单3 :(香蕉,汉堡包,冰淇淋)

因此将有3 X 3 X 3 = 27个可能的输出,它们是:

我想吃香蕉

我想吃一个汉堡包

... 等等

我很确定没有一个衬里可以做到这一点,但有一种简单的方法可以使用简单的unix构建块实现它吗?

1 个答案:

答案 0 :(得分:1)

虽然不是技术上的shell命令,但是Bash脚本只需嵌套for循环就可以轻松完成。

#include <unordered_map>
#include <sstream>
#include <string>
#include <iostream>

class FilePaths {
    std::unordered_map<std::string, std::string> m_mStrFilePaths;

public:
    FilePaths();
    ~FilePaths();
    std::string getPath( const std::string& strId ) const;
    void addPath( const std::string& strFilePath );

private:    
    FilePaths( const FilePaths& c );
    FilePaths& operator=( const FilePaths& c );

}; // FilePaths


static unsigned uId = 0; // Initialize to 0 - This should be in your class.cpp file!
FilePaths::FilePaths() {
    m_mStrFilePaths.clear();
} // FilePaths

FilePaths::~FilePaths() {
    m_mStrFilePaths.clear();
} // ~FilePaths

std::string FilePaths::getPath( const std::string& strId ) const {
    if ( strId.empty() ) {
        // Return Error Or If In Try Catch Block Throw Error    
        return std::string();
    }

    std::unordered_map<std::string, std::string>::const_iterator it = m_mStrFilePaths.find( strId );
    if ( it == m_mStrFilePaths.cend() ) {
        // Not Found
        // Return Error Or If In Try Catch Block Throw Error    
        return std::string();
    }
    return it->second;
} // getPath

void FilePaths::addPath( const std::string& strFilePath ) {
    if ( strFilePath.empty() ) {
        // Return Error Or If In Try Catch Block Throw Error    
        return;
    }

    std::ostringstream strStream;
    strStream << "Id_" << ++uId;

    m_mStrFilePaths[strStream.str()] = strFilePath;
} // addPath

int main( int argc, char** argv ) {

    FilePaths paths;

    // I have double slashes in the strings here because of the escape sequence /P
    paths.addPath( std::string( "C:\\PathA" ) );
    paths.addPath( std::string( "C:\\PathB" ) );
    paths.addPath( std::string( "C:\\PathC" ) );

    std::string myPath = paths.getPath( std::string( "Id_2" ) );

    std::cout << myPath << std::endl;

    // Has No Meaning - I put a break point on this line to stop execution from closing the console.
    std::cout << "Pause On This Line" << std::endl;

    return 0;
 } // main