小编Kin*_*Jnr的帖子

二维数组值C++

我想声明一个2D数组并为其赋值,而不运行for循环.

我以为我可以使用以下想法

int array[5] = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)

这也适用于初始化2D阵列.但显然我的编译器不喜欢这个.

/*
 1   8  12  20  25
 5   9  13  24  26
*/

#include <iostream.h>

int main()
{
    int arr[2][5] = {0};   // This actually initializes everything to 0.
    arr [1] [] = {1,8,12,20,25}; // Line 11
    arr [2] [] = {5,9,13,24,26};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

J:\ CPP\Grid> bcc32.exe Grid.cpp

Borland C++ 5.5.1 for Win32版权所有(c)1993,2000 Borland

Grid.cpp:

错误E2188 Grid.cpp 11:函数main()中的表达式语法

错误E2188 Grid.cpp 12:函数main()中的表达式语法

警告W8004 Grid.cpp 14:'arr'被分配一个从未在函数main()中使用的值

*编译中的2个错误*

请帮助确定使用我的一组值初始化2d数组的正确方法.

c c++ initialization shortcut multidimensional-array

36
推荐指数
4
解决办法
17万
查看次数

前导1表示签名int中的负数是从哪里出现的?

即使我读过一些文章,说大多数2的补码用于表示有符号整数中的负数,这是最好的方法,

但是由于某些原因,我(在下面)这个(下面)卡在我的头脑中,在不知道它的历史的情况下无法摆脱它

"使用带符号的int时,使用前导位为1来表示负数."

我在网上和StakOverflow上看了很多帖子,2的补码是表示负数的最佳方式.但我的问题不是关于最好的方式,而是关于历史或"领先位"概念出现在哪里然后消失?

PS:也只是不是我,其他一些人也对此感到困惑.

编辑 - 1我在上面提到的一个例子中描述了所谓的前导1方法: 为什么两个补码用于表示负数?

现在我明白了,1的MSB表示负数.这本质上是2的补充而不是任何特殊方案.

例如.如果不是第1位,我们不能说1011代表-5还是+11.

感谢:詹姆斯德林,奥利查尔斯沃思,利斯特先生提出恳求问题,让我意识到正确的答案.

Rant:我认为有很多团体/人都被教导或被认为(错误地)认为1011评估为-3.1表示 - 和011表示3.

那些问"我的问题是什么......"的人可能从他们学到的第一个实例中学到了正确的2的补充方式,并且没有接触到这些错误的答案.

math binary unsigned signed

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

是否有可能在编译期间错误检查case语句选项?

如何以这样的方式重写此代码:只要用户通过有效方向,就会始终显示正确的状态(CA,AL等)字符串.

即如何确保编译时枚举方向中的每个enum_types都有一个有效的case语句,而不是运行时?

对于.例如.我故意评论了东方的案例陈述.有没有办法在编译时捕获它.

我的直觉是(否)这就是语言提供默认情况的原因,因此这可能是不可能的.但我会把它留给专家.

#include <iostream>
#include <string>

using namespace::std;

typedef enum 
{
    min_dir = -1,
    north,
    south,
    east,
    west,
}directions;

directions get_direction( string user_choice)
{
    if(user_choice == "north")
    {
        return north;
    }
    else if (user_choice == "south")
    {
        return south;   
    }
    else if (user_choice == "east")
    {
        return east;    
    }
    else if (user_choice == "west")
    {
        return west;    
    }
    else 
    {
        return min_dir;
    }
}

int main()
{
    string user_direction;
    cout << "Enter direction\n";
    cin >> user_direction;

    directions my_dir …
Run Code Online (Sandbox Code Playgroud)

c++ runtime switch-statement compile-time

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

数据结构表示具有"多组合"键的查找表

您将如何设计用于查找的表数据结构?

我基本上必须代表下表

Country             Activity        Legal_Age
European            Drink           18
European            Drive           21
American            Drink           21
American            Drive           18
Run Code Online (Sandbox Code Playgroud)

这里我的密钥是(国家/地区和活动),价值是Legal_age.

我想到使用std :: map来解决这个问题(到个别地图),如下所示.

national_Activity_age_map

European            European_Activity_age_map
American            American_Activity_age_map
Run Code Online (Sandbox Code Playgroud)

European_Activity_age_map

Drink           18
Drive           21
Run Code Online (Sandbox Code Playgroud)

American_Activity_age_map

Drink           21
Drive           18
Run Code Online (Sandbox Code Playgroud)

但问题是,随着原始表的列数不断增加,要添加和维护的映射数量不断增加.

假设美国选择为美国公民和非公民分别饮酒.然后我将不得不添加新的映射并修改现有的映射.

是否有一种简单而干净的方式在DataStructure中表示此数据,该数据采用多因子密钥并生成一个值?

有一些类似的问题如下,但没有解决我的具体问题. 您将使用什么数据结构来表示此数据格式?

更新:我不能在工作中使用Boost功能,因为它必须被移植(或类似的东西).是否有我可以使用的C++(gcc 4.1.2)功能.

c++ oop

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

将字符串插入向量

我收到编译错误.我正在尝试向向量添加字符串并将其保持为"排序顺序".

XYZ是我的班级.addPortEntry

class XYZ
{
    public:
        portListFile(string sTmp);
        void addPortEntry(string sPortName, string sDirection);
    private:
        string sPortListFileName;
        vector <string> v_input_ports;
    ...
};

void XYZ::addP(string sP, string sDir)
{
    if(sDir == "in")
    {
        v_input_ports.insert(sP);   // Line 42
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

错误:

XYZ.cpp: In member function ‘void XYZ::addP(std::string, std::string)’:
XYZ.cpp:42: error: no matching function for call to ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::insert(const char [10])’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:93: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, …
Run Code Online (Sandbox Code Playgroud)

c++ string insert vector

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

这是在C或Perl中终止for循环的合适方法吗?

我在代码库中看到了一段类似的perl代码,想知道这个(设置i=100)是否是一个摆脱for循环的好方法?这有什么陷阱吗?

int a[100];

...

bool check_if_array_contains_29()
{
    bool result = false;
    for(int i=0; i<100; ++i)
    {
        if(a[i] == 29)
        {
            result = true;
            i = 101;
        }
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

这更像我会做的事情.

bool check_if_array_contains_29()
{
    bool result = false;
    for(int i=0; i<100 && !result; ++i)
    {
        if(a[i] == 29)
        {
            result = true;
        }
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

编辑-1:

我不是在寻找perl中的oneliner来实现功能.真正的代码(功能)要复杂得多.这只是我简化的一个例子来解释我的观点(提前终止for循环).

c perl coding-style

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

Visual Studio 2010 - 数据段和堆栈内存相同

我发现一个常量的文字get放在程序的数据段中(来自SO)并且是只读的,因此行"s [0] ='a'"会导致错误,这实际上发生在我取消注释该行并运行.但是,当我查看MS VS中的内存窗口时,变量全部放在内存中.我很好奇他们(编译器)如何强制执行对's'的只读访问?

#include <iostream>

int main(void)
{
      char *s = "1023";
      char s_arr[] = "4237";
      char *d = "5067";
      char s_arr_1[] = "9999";
      char *e = "6789";
      printf("%c\n", s[0]);
//      s[0] = 'a'; This line would error out since s should point to data segment of the program
      printf("%s\n", s);
      system ("pause");
}

0x002E54F4  31 30 32 33 00 00 00 00 34 32 33 37 00 00 00 00  1023....4237....
0x002E5504  35 30 36 37 00 00 …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-memory memory-segmentation

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

fopen返回null - Perror打印无效参数

简单的fopen操作似乎不起作用.perror返回 - 参数无效.可能有什么不对.

我在R:中有一个名为abc.dat的ascii文件.

int main()
{
    FILE *f = fopen("R:\abc.dat","r");
    if(!f)
    {
        perror ("The following error occurred");

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

输出:

发生以下错误:参数无效.

c file-io

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

我应该在头文件中哪里声明我的私有类级静态常量?

我应该在头文件中指定我的类级静态常量?

class ABC
{
public:
    enum A1
    {
    };

    //Manager functions
    ABC(int x )
    {
        m_x = x;
    };
    viirtual ~ABC();

protected:
    int data;

private:
    //Typedef
    typedef std::pair(int, int) INT_PAIR;
    typedef std::pair(int, int) INT_PAIR1;
    ...
    //functions
    void increment_x();
    //Member data
    int m_x;
    ... whole lot of other data
}
Run Code Online (Sandbox Code Playgroud)

我应该在这个类声明(ABC.h)中声明一个私有静态const变量,如版本号?

static const std::string version;
Run Code Online (Sandbox Code Playgroud)

它究竟适合哪里?它不是真正的成员数据,因为它是静态的.(不是每个对象)

编辑 - 1:

这些变量是否有特定的优先级?它们是在开始时(在ABC类之后的第一个开口大括号之后?或者在我的代码段中的私有关键字之后?)(或者)是在typedef之后吗?

当然我会在我的abc.cpp文件中提到const std :: string version ="10";

编辑2:我期待卢卡斯提到的答案.(请提供有效的推理)

在类似我下面提到的类声明中,应该放置静态变量吗?

请不要提供答案,提到decl需要在.cpp文件中的.h文件和定义中. - 我已经知道了.

c++ static-members

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

嵌套for循环,在循环外部进行初始化

#include <stdio.h>

int main()
{
    int i = 1, j = 1;

    for(i; i<=5; i++)
    {
        for(j; j<=5; j++)
        {
            printf("%d - %d\n",i, j);
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码只运行一次外部for循环.输出是:

1 - 1
1 - 2
1 - 3
1 - 4
1 - 5
Run Code Online (Sandbox Code Playgroud)

但是当我进行以下更改时,printfs会打印预期的25次.

    for(i=1; i<=5; i++)
    {
        for(j=1; j<=5; j++)
        {
            printf("%d - %d\n",i, j);
        }
    }
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下这种行为吗?

c nested-loops

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

Perl - myprint()或重载打印命令的新定义

我是Perl的新手.我正在编写一些脚本,并希望定义我自己的打印调用myprint(),它将根据一些标志打印传递给它的东西(详细/调试标志)

open(FD, "> /tmp/abc.txt") or die "Cannot create abc.txt file";
print FD "---Production Data---\n";
myprint "Hello - This is only a comment - debug data";
Run Code Online (Sandbox Code Playgroud)

有人可以帮我提供一些示例代码来实现myprint()功能吗?

printing perl

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