小编Bjö*_*lex的帖子

C++使用null部分填充数组

NoobQuestion:我听说填充char数组可以使用null char尽早终止.这是怎么做到的?我搜索了每一个谷歌的结果,但仍然空手而归.

c++ arrays null

1
推荐指数
1
解决办法
755
查看次数

C++ - #include"filename"

本教程中,它提到了以下内容#include "filename":

#include"filename"告诉编译器在包含执行#include的源文件的目录中查找文件.如果失败,它将与有角度的托架案件完全相同.

粗体字体句子是什么意思?

谢谢.

c++ include

1
推荐指数
1
解决办法
1366
查看次数

在Python和C中实现一个函数

在Python中实现一个函数和在C中实现它然后从Python调用它之间是否存在差异(就执行时间而言)?如果是这样,为什么?

c python compilation

1
推荐指数
1
解决办法
317
查看次数

矢量容量和储备

请考虑以下代码段:

std::vector<int> v;
v.reserve(100);
v.insert(v.end(), 100, 5);

v.erase(v.begin(), v.end());

std::cout << v.capacity << std::endl;
Run Code Online (Sandbox Code Playgroud)

打印出来100.这是否意味着向量仍然保留了100个内存位置?是否需要reserve(0)在调用erase(begin,end)向量后调用,以放弃向量所持有的所有空间?

c++ memory-leaks stl vector

1
推荐指数
1
解决办法
2949
查看次数

从java方法返回一个字节数组

我有一个字节数组,我传递给一个函数nibbleSwap.nibbleSwap对于阵列中的每个字节值,将第4个位与第2个4位进行交换.交换后,我必须返回交换的字节值.如果我只是打印字节数组我能够得到正确的值但是当我返回交换的字节数组时,它不会打印正确的值.
我的代码是这样的:

private static byte[] nibbleSwap(byte []inByte){
        int []nibble0 = new int[inByte.length];
        int []nibble1 = new int[inByte.length];
        byte []b = new byte[inByte.length];

        for(int i=0;i<inByte.length;i++)
        {
                nibble0[i] = (inByte[i] << 4) & 0xf0;
                 nibble1[i] = (inByte[i] >>> 4) & 0x0f;
                b[i] =(byte) ((nibble0[i] | nibble1[i]));
                /*System.out.printf(" swa%x ",b[i]); ---   if i do this by changing the return to void i get the correct output.
        */
                   }

        return b;
    }  
Run Code Online (Sandbox Code Playgroud)

例如.valuebyte[]contains:91,19,38,14,47,21,11 我希望函数返回一个包含的数组19,91,83,41,74,12,11.另外,我可以通过将返回类型更改为String来将其作为String返回,因为当我这样做并打印它时,我得到了交换字节值的整数值吗?

请帮忙!! …

java string

1
推荐指数
1
解决办法
2万
查看次数

C++ MAC OS X无法写入〜/ Library/Application Support/<appname>

如果我在.app捆绑包中保存文件,它会保存正常,但Apple建议将文件保存在Application Support下

~/Library/Application Support/appname
Run Code Online (Sandbox Code Playgroud)

要么

~/Library/Application Support/bundleid
Run Code Online (Sandbox Code Playgroud)

我试过两个,但我总是得到一个例外.我正在获得应用程序支持的路径,即

/Users/myname/Library/Application Support/com.company.appname/
Run Code Online (Sandbox Code Playgroud)

要么

/Users/myname/Library/Application Support/AppName/
Run Code Online (Sandbox Code Playgroud)

com.company.appname在我的内部正确指定info.plist,AppNameproduct AppName.app,所以路径似乎是正确的.

{
    FilepathProcessor::pathForFile(fpath, "menustate", ".sav", 
                                   PATH_TO_DESTINATION_SAVE_LOAD_FOLDER);

    std::ofstream file;
    file.exceptions(std::ofstream::eofbit | std::ofstream::failbit | 
                    std::ofstream::badbit);
    INFO_ARG("prepare to save to '%s'", fpath.c_str());

    try {
        file.open(fpath.c_str(), std::ios::out | std::ios::binary | 
                  std::ios::trunc);
        ERROR_IF_ARG(!file.is_open(), "couldnt open file for saving at '%s'",
                     fpath.c_str(), return);

        //will pass this point

        //exception happens by first write
        WRITE_INT_TO_BINFILE(file, episode);

        //...

    } 
    catch (std::ofstream::failure e) {
        ERROR_IF_ARG(true, "exception %s", …
Run Code Online (Sandbox Code Playgroud)

c++ macos file std

1
推荐指数
1
解决办法
1195
查看次数

无法在存储在容器中的指针上调用公共方法

奇怪的是,我在C++中遇到以下代码的编译错误.

class A
    {
    public:
        void sayHai()
        {
            cout << "\n Hai";
        }
    };

    int main(int argc, char** argv)
    {            
        vector< A* > vectorA;
        vectorA.push_back(new A());
        for (vector< A* >::iterator iter = vectorA.begin(); 
             iter != vectorA.end(); 
             ++iter)
            *iter->sayHai();
    }
Run Code Online (Sandbox Code Playgroud)

这里我在向量中存储指向A类的指针.当我尝试调用公共方法时,我收到以下编译错误.

VectorExample.cpp: In function 'int main(int, char**)':
VectorExample.cpp:30: error: request for member 'sayHai' in 
    '* iter.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> 
    [with _Iterator = A**, _Container = std::vector<A*, 
    std::allocator<A*> >]()', which is of non-class type 'A*'
Run Code Online (Sandbox Code Playgroud)

有谁遇到过这样的情况?为什么这被视为编译错误?什么应该是解决这个问题的必要方法?

我用以上编译了以上内容 g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)

c++ stl vector

1
推荐指数
1
解决办法
108
查看次数

如何使用python读取csv文件中包含逗号的单个值

我有一个包含以下值的csv文件

"Spam2,pank",Spam3,Spam6,Spam7
Spam1,Spam5,Spam0,Spam9
Run Code Online (Sandbox Code Playgroud)

我使用以下python代码来读取特定列

for line in open('D:\eggs2.csv','rb'):
    columns = line.split(",")
    print columns[0]
Run Code Online (Sandbox Code Playgroud)

它给出的输出是: -

"Spam2
Spam1
Run Code Online (Sandbox Code Playgroud)

虽然我期待:

Spam2,pank
Spam1
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.

python csv

1
推荐指数
1
解决办法
3806
查看次数

C++复制多维向量

我在复制多维向量时遇到问题,我尝试了很多东西,但这是最后一个:

vector < vector < int > > a;
vector < vector < int > > b;
a.resize(10);
b.resize(10);
a[0][0] = 123;
copy( a.begin(), a.end(), back_inserter(b) );
cout << b[0][0];
Run Code Online (Sandbox Code Playgroud)

我正在尝试做一个递归循环,在10个移动中计算网格中所有可能的路径.我正在尝试创建一个向量current_path,该向量将保存每个递归的当前路径,当current_path有10次移动时,它会将数据从中复制current_pathall_paths.

网格如下:

0  1  2 3
4  5  6 7
8  9  10 11
12 13 14 15
Run Code Online (Sandbox Code Playgroud)

你只能移动到你触摸的方格,从0开始可以移动到1,4和5.从1到3,4,5,6等.

主要的想法是复制current_path到下一个函数调用(递归),这样就可以保持到那curren_path一点,直到它完全(10个步骤).它是从复制后current_pathall_paths我想我已经删除了current_path

我知道如何有效地计算所有步骤,但我无法复制current_path和propably和我怎么添加current_pathall_paths当我在10级?

c++ copy vector multidimensional-array

1
推荐指数
2
解决办法
1万
查看次数

如何在多重继承中使用namedtuples

是否可以创建一个继承自多个实例的类namedtuple,或者创建具有相同效果的类(具有组合基类型字段的不可变类型)?我还没有找到办法.

这个例子说明了这个问题:

>>> class Test(namedtuple('One', 'foo'), namedtuple('Two', 'bar')):
>>>    pass

>>> t = Test(1, 2)
TypeError: __new__() takes 2 positional arguments but 3 were given

>>> t = Test(1)
>>> t.foo
1
>>> t.bar
1
Run Code Online (Sandbox Code Playgroud)

问题似乎是namedtuplesuper用于初始化其基类,如创建一个时可以看到:

>>> namedtuple('Test', ('field'), verbose=True)
[...]    
class Test(tuple):
[...]
    def __new__(_cls, field,):
        'Create new instance of Test(field,)'
        return _tuple.__new__(_cls, (field,))
Run Code Online (Sandbox Code Playgroud)

即使我考虑编写自己的版本namedtuple来解决这个问题,但如何做到这一点并不明显.如果namedtuple类的MRO中有多个实例,则它们必须共享基类的单个实例tuple.要做到这一点,他们必须协调namedtuple使用基本元组中哪个索引范围.

有没有更简单的方法来实现与namedtuple类似或类似的多重继承?有人已经在某处实现了吗?

python inheritance multiple-inheritance namedtuple

1
推荐指数
2
解决办法
3056
查看次数