相关疑难解决方法(0)

可变长度的非POD元素类型'string'(又名'basic_string <char>')c ++

我在我的c ++代码中得到此错误非POD元素类型string(aka basic_string<char>)的可变长度数组.

string words[numWords];
Run Code Online (Sandbox Code Playgroud)

如果我摆脱numWords并输入一个数字,这工作正常,但如果我把相同的数字放在一个变量,它给我Variable length array of non-POD element type 'string' (aka 'basic_string<char>')错误,我已经这样做过,它在视觉工作室工作,但我现在在Xcode中尝试过它不起作用.我已经尝试过使用矢量,但我无法让它们存储任何数据,它们只是空白.

对于那些问我这是我的矢量代码的人应该都在那里

char ch;

ifstream repFile("//Users//bobthemac//Documents//c++asignment//c++asignment//test1.txt");

while(repFile.get(ch))
{
    if(ch == ' ' || ch == '\n' || ch == '\t')
    {
        numWords++;
    }
}

vector<string> words (numWords);

while(repFile >> x)
    words.push_back(x);
repFile.close();
Run Code Online (Sandbox Code Playgroud)

c++ arrays string xcode xcode4

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

使用模板和基类实现灵活的数组成员

在C99中,您通常会看到以下模式:

struct Foo {
    int var1;
    int var2[];
};

Foo * f = malloc(sizeof(struct Foo) + sizeof(int)*n);
for (int i=0; i<n; ++i) {
    f->var2[i] = p;
}
Run Code Online (Sandbox Code Playgroud)

但不仅是这个糟糕的C++,它也是非法的.

您可以在C++中实现类似的效果,如下所示:

struct FooBase {
    void dostuff();

    int var1;
    int var2[1];
};

template<size_t N>
struct Foo : public FooBase {
    int var2[N-1];
};
Run Code Online (Sandbox Code Playgroud)

虽然这将工作(你可以访问FooBase的方法var2[2],var2[3]等等),它依赖于Foo作为标准配置,这是不是很漂亮.

这样做的好处是非模板化函数可以Foo*通过采用FooBase*和调用操作的方法来接收任何没有转换的函数var2,并且内存是连续的(这可能是有用的).

有没有更好的方法来实现这一点(这是合法的C++/C++ 11/C++ 14)?

我对两个简单的解决方案不感兴趣(包括基类中的额外指针到数组的开头,并在堆上分配数组).

c++ flexible-array-member

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

运算符重载矢量控股复数

#include<iostream>
#include<cmath>
#include<complex>
#include<vector>

using namespace std;

vector<complex<double> > Compute_wn(int n);
int main()

{
    Compute_wn(8);
    return 0;
}


vector<complex<double> > Compute_wn(int n)
{
    vector<complex<double> > V[n];
    for(int i = 0; i<n; i++)
    {
        V[i] = complex<double>(cos(2.0*M_PI*double(i)/double(n)),sin(2.0*M_PI*double(i)/double(n)));
        cout<<V[i]<<"\t";
    }
    cout<<"\n";
}
Run Code Online (Sandbox Code Playgroud)

我是C++的新手.当我编译这段代码时,我得到的错误是错误:没有可行的重载'='.(V[i] = complex<double>(cos(2.0*M_PI*double(i)/double(n)),sin(2.0*M_PI*double(i)/double(n)));)我不明白我需要如何为一个复数类型为double的向量重载运算符'='.据我所知,向量是复数的向量,我应该能够直接赋值给V [I].

c++

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

如何制作包含数组的向量?

我试图学习C ++中的存储桶排序算法,发现以下代码:

// C++ program to sort an array using bucket sort 
#include <iostream> 
#include <algorithm> 
#include <vector> 
using namespace std;

// Function to sort arr[] of size n using bucket sort 
void bucketSort(float arr[], int n)
{
    // 1) Create n empty buckets 
    vector<float> b[n];

    // 2) Put array elements in different buckets 
    for (int i = 0; i < n; i++)
    {
        int bi = n * arr[i]; // Index in bucket 
        b[bi].push_back(arr[i]);
    }

    // 3) Sort individual …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting stdvector c++11

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

为什么我可以在 C++ 中动态创建静态数组?

我只是在浏览一些简单的 C++ 概念。我喜欢认为我知道动态和静态数组之间的区别。但是当我运行以下代码时:

`

#include <iostream>
using namespace std;
int main()
{
    int size;
    cout<<"enter size: ";
    cin>>size;
    int arr[size];
    cout<<"enter array values: ";
    for(int i=0;i<size;i++)
        cin>>arr[i];
    for(int i=0;i<size;i++)
        cout<<arr[i]<<" ";
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

`

它不会给我一个错误。它不应该让我使用用户输入的大小创建一个静态数组吗?

c++ arrays dynamic-arrays

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

整数数组中每个元素的数字总和

我想计算数组中每个元素的数字总和。问题在于这段代码只计算数组中奇数索引 (1,3,5...) 的总和。在控制台中,它显示偶数索引的一些随机数(0,2,4 ...)

谁能告诉我是什么问题?

是的,我需要将它用作数组

以下是输出值:

Enter how many numbers you want to calculate sum of digits: 5
Enter those numbers: 12
Enter those numbers: 33
Enter those numbers: 44
Enter those numbers: 22
Enter those numbers: 33
Sum of 0 number is: 4
Sum of 1 number is: 6
Sum of 2 number is: 40
Sum of 3 number is: 4
Sum of 4 number is: 11730950
Run Code Online (Sandbox Code Playgroud)
#include <iostream>


int main(int argc, char** argv) 
{
    int n;
    int …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sum digits

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

在 C++ 中展平多维向量

我想用 C++ 编写一个通用函数来展平所提供的任何多维向量。该方法的签名如下:

template <class U, class T>
void flatten(U* out, T& inp, int* dims, int n){
  // out is the flattened output
  // inp is some multidimensional vector<vector...<U>>
  // dims is an array of the dimensions of inp
  // n is the number of dimensions of inp
  int ticker[n];
  int prodLimit = 1;
  int prod = 0;
  
  // calculate prodLimit as product of elements in dims and initialize ticker
  for (int i=0; i<n; i++){
    ticker[i] = 0;
    prodLimit *= …
Run Code Online (Sandbox Code Playgroud)

c++ vector multidimensional-array

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

如何在C++中创建一个数组,其长度等于用户输入?

我知道我不能创建这样的数组:

int main () {
    int length;
    std::cin >> length;
    int array [length] = {};
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以吗?

c++ arrays

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

什么是VLA类型的表达?

我试图理解sizeof运算符的工作,我遇到了 这个 问题.以下是该问题的代码

#include <stdio.h>

int main() {
    int i = 0;
    int a[i];
    printf("%zu\n",sizeof(a[i++]));
    printf("%d\n",i); // Here, print 0 instead of 1
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

数组a这里是可变长度但是当它用作sizeof运算符的操作数时,变量i不会递增.

一些评论和答案说这a[i++]不是VLA类型,并建议op应该使用2D VLA来查看副作用(sizeof评估其oprand).

我不清楚为什么a[i++]不符合VLA表达的条件.我认为这与我们可以在传递给一个函数时保留第一个未指定数组的事实有关.

所以问题一般是什么被认为是VLA表达?

c sizeof variable-length-array

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

查找字符串中的数字(0-9)的频率

我在C中查找字符数组中数字频率的代码没有给出所需的输出.

int main() {
    int n;
    scanf("%d",&n);
    char str[n];
    int f[10],i,j,ns;
    gets(str);
    for(i=0;i<10;i++)
    {
        f[i]=0;
        for(j=0;j<strlen(str);j++)
        {
            ns=str[j]-'0';
            if(i==ns)
                f[i]+=1;
        }
    }
    for(i=0;i<10;i++)
        printf("%d ",f[i]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我输入字符串

1wjhue73b38409598
Run Code Online (Sandbox Code Playgroud)

我得到频率为(0-9)的输出:

1 0 0 2 1 1 0 1 2 2 
Run Code Online (Sandbox Code Playgroud)

而不是频率1代表'1'.我哪里错了?

c

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