小编ana*_*ciu的帖子

二维数组作为指向字符数组的指针

我正在使用 C 中的一些代码,并且我试图理解指针和数组之间的关系。您可能知道,当我想制作数组时,可以这样做:

char * arr = "abc";
Run Code Online (Sandbox Code Playgroud)

或者

char arr[] = {'a','b', 'c'};
Run Code Online (Sandbox Code Playgroud)

但是当我想做二维数组时。必须这样做

char arr[3][10];
Run Code Online (Sandbox Code Playgroud)

当我尝试将字符串加载到它时,为什么这样的声明会崩溃。

char * names[3];

for ( int i = 0; i < 3; i++ ) {
    printf("Enter name %d: ", i+1 );
    scanf("%s", names[i]);
}
// print names
printf("\nEntered names are: \n");
for ( int i = 0; i < 3; i++ ) {
    printf("%s\n", names[i] );
}
Run Code Online (Sandbox Code Playgroud)

应该是二维数组吧?因为数组基本上是指针。你能解释一下吗?谢谢。

c arrays pointers

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

为什么需要在索引中添加“0”才能访问数组值?

我对这一行感到困惑:

sum += a[s[i] - '0']; 
Run Code Online (Sandbox Code Playgroud)

给出一些上下文,这是代码的其余部分:

#include <iostream>

using namespace std;

int main() {

    int a[5];
    for (int i = 1; i <= 4; i++)
        cin >> a[i];
    string s;
    cin >> s;
    int sum = 0;
    for (int i = 0; i < s.size(); i++)
        sum += a[s[i] - '0'];
    cout << sum << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ char indices character-set

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

我无法理解 std::istream_iterator 的用法

我无法理解下面的代码。

(来自https://www.boost.org/doc/libs/1_74_0/more/getting_started/unix-variants.html

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
Run Code Online (Sandbox Code Playgroud)

该网页没有对代码进行任何解释。

我无法理解的是功能线std::for_each

std::for_each定义如下。

template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn);
Run Code Online (Sandbox Code Playgroud)

所以first就是in(std::cin)last就是in(),就是function这个cout陈述。

谁能向我解释示例代码中的语法和含义firstlast

迭代first器似乎是用初始值构造的,但是最后一个值std::cin有什么用呢?in()

我也无法理解该_1部分。

该程序输出3 …

c++ boost iterator istream-iterator

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

将字符串转换为双精度保持四舍五入为整数

我正在尝试将string十进制数转换为double,但是当我使用该atof()函数时,我的数字最终会四舍五入为整数。

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

135428
Run Code Online (Sandbox Code Playgroud)

我想要:

135427.7
Run Code Online (Sandbox Code Playgroud)

c++ string double rounding type-conversion

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

如何初始化对象数组?

我已经编写了这段代码,但是当我尝试初始化一个Critter对象数组并且不知道它们是关于什么时出现了一些错误。

我的代码:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Critter {
private:
    string crName;
public:
    Critter(string = "Poochie");
    string getName() const { return crName; }
};

Critter::Critter(string n) {
    crName = n;
}

int main() {
    Critter c[10] = { "bob","neo","judy","patrik","popo" }; //here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Critter {
private:
    string crName;
public:
    Critter(string = "Poochie");
    string getName() const { return crName; }
};

Critter::Critter(string …
Run Code Online (Sandbox Code Playgroud)

c++ arrays runtime-error visual-studio object-initialization

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

C++ 优先级和关联性

这段代码:

int scores[] {1,2,3,4};
int *score_ptr {scores};  
//let's say that initial value of score_ptr is 1000
std::cout<<*score_ptr++;
Run Code Online (Sandbox Code Playgroud)

产生输出:

1
Run Code Online (Sandbox Code Playgroud)

As*++具有相同的优先级,然后结合性是从右到左,我们不应该++先应用运算符,即先增加指针然后*(取消引用)它吗?

因此,相应地score_ptr将增加到1004然后取消引用它将给出分数的第二个元素,即2.

这如何以及为什么给我输出1而不是2

c++ computer-science pointers operator-precedence

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

相似的代码输出不同的结果

以下代码的输出是0.0000000

#include <stdio.h>

int main() {
    float x;
    x = (float)3.3 == 3.3;

    printf("%f", x);

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

而此代码输出1.000000

int main() { 
    float x; 
    x = (float)3.5 == 3.5; 

    printf("%f", x); 

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

2个代码唯一的区别是比较中的值,但是结果不一样,这是为什么呢?

c floating-point double

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

Angular 不会自动显示字符串数组中的更改

let signalRServerEndPoint = 'https://localhost:44338';
this.connection = $.hubConnection(signalRServerEndPoint);
this.proxy = this.connection.createHubProxy('MessagesHub');

this.proxy.on("ReceiveMessage", (message) => {
  console.log(message); //LOG IS OKAY
  this.listMessages.push(message); // PUSH IS OKAY
  console.log(this.listMessages); // LOG IS OKAY IS OKAY
});
Run Code Online (Sandbox Code Playgroud)

listmessages 是一个 string[] 数组。console.log() 工作正常, this.listMessages.push(message) 工作正常,因为第二个 console.log 显示正确的字符串数组。但我的问题是在我的用户界面中,它不会自动填充新的 listMessages。仅当我在文本框中键入内容或再次单击发送按钮时,它才会显示新填充的数组,然后显示我之前发送的最新数组。任何人都可以帮助我这有什么问题吗?

let signalRServerEndPoint = 'https://localhost:44338';
this.connection = $.hubConnection(signalRServerEndPoint);
this.proxy = this.connection.createHubProxy('MessagesHub');

this.proxy.on("ReceiveMessage", (message) => {
  console.log(message); //LOG IS OKAY
  this.listMessages.push(message); // PUSH IS OKAY
  console.log(this.listMessages); // LOG IS OKAY IS OKAY
});
Run Code Online (Sandbox Code Playgroud)

javascript signalr typescript angular

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

关于函数类型冲突的问题

我是 C 的学生,我的函数中的变量类型有问题。这是一个计算资本以及一定跨度内的利率的函数,它表示“capital_a_terme”存在冲突类型。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    scanf("%f", &capital_initial);
    printf("Saisir le taux d'interet fixe.\n");
    scanf("%f", &taux_interet_fixe);
    printf("Saisir le nombre d'annee de placement.\n");
    scanf("%f", &nb_annee_placement);
    printf("le capital a terme vaut : %f.\n", capital_a_terme(capital_initial, taux_interet_fixe, nb_annee_placement));
}

float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement)
{
    if (nb_annee_placement == 0)
    {
        return capital_initial;
    }
    else
    {
        return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / 100, taux_interet_fixe, nb_annee_placement - 1));
    }
}
Run Code Online (Sandbox Code Playgroud)

c

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

C++11 字符数组初始化和字符串文字

在 C++11 中,char指针不能直接初始化为字符串文字。
在早期版本的 C++ 中,我可以毫无问题地执行此操作。

如果允许使用以下代码:

char arr[] = "Hello";
char *p_str1 = arr;  //allowed
Run Code Online (Sandbox Code Playgroud)

那么为什么下面的代码不允许呢?

char *p_str3 = "Hello"; //Not allowed
Run Code Online (Sandbox Code Playgroud)

注意:我知道添加const可以修复。但我需要知道原因。

c++ arrays pointers string-literals c++11

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