小编Mat*_*ieu的帖子

矢量中对象的地址发生了变化

我用A对象填充了一个向量,然后将这些对象地址存储在multimap[1]中,但是打印消息显示对向量中存储的对象的引用发生了变化[2].你明白为什么吗?以及如何避免任何变化.

//[1]
vector<A> vec; 
multimap<const A*, const double > mymultimap;

for (const auto &a : A) {
  double val = a.value();
  vec.push_back(a);
  mymultimap.insert(std::pair<const A*, const double >( &vel.back(), val)); 

  // displaying addresses while storing them    
  cout<<"test1: "<<&vec.back()<<endl;

}

//[2]
// displaying addresses after storing them
for(auto &i : vec)
    cout << "test2: " << &i <<endl;
Run Code Online (Sandbox Code Playgroud)

结果:

test1: 0x7f6a13ab4000  
test1: 0x7f6a140137c8  
test2 :0x7f6a14013000  
test2 :0x7f6a140137c8  
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

grep文件中的变量字符串

我必须在文件中找到这个字符串:

200 https://www.example.example
Run Code Online (Sandbox Code Playgroud)

值200是随机的,我必须找到每个HTTP返回码(ex 200,301,404等...等)

我怎么能只用返回代码变量grep这个字符串(我不想在grep命令中指定每个返回代码)?

cat file.txt | grep "*** http*" 
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

regex linux bash grep match

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

如何在vs code中安装和使用Openmp

我已经在 vscode 中正确安装并运行了 c/c++,我的 gcc 版本是 8.2.0 并且我已经安装了 MinGw

我使用 VS code 来运行我的 C 程序

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

int main(int argc, char* argv[])
{
    int nthreads, tid;
    {
        tid = omp_get_thread_num();
        printf("welcome to GFG from thread = %d\n", tid);
        if (tid == 0){
            nthreads = omp_get_num_threads();
            printf("number of threads = %d\n", nthreads);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这没有用,因为

[Running] cd "c:\cexam\" && gcc openmp_helloword.c -o openmp_helloword && "c:\cexam\"openmp_helloword
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0xf): undefined reference to `omp_get_thread_num'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\WinstonLi\AppData\Local\Temp\ccAAWXl3.o:openmp_helloword.c:(.text+0x33): undefined reference to …
Run Code Online (Sandbox Code Playgroud)

c gcc openmp visual-studio-code

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

使用memcmp Vs. == in c(嵌入式)和意外错误

我一直听说使用==运算符来比较字符可能会导致一些问题.

我的问题是 - 什么样的问题?

例如,使用之间有什么区别:

if (Text[0] == 'A') { ; }
Run Code Online (Sandbox Code Playgroud)

if (!memcmp(Text, "A", 1)) { ; }
Run Code Online (Sandbox Code Playgroud)

c embedded syntax

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

使用for循环在memset()和initializinng数组元素之间的区别?

当我memsetprintNGE函数内部使用时,我也得到了正确的结果,当我将数组元素初始化为-1时main(),我得到了正确的结果.

但是当我printNGE使用for循环在函数中初始化相同时,我得到了错误的答案.由于某种原因,似乎数组元素的初始值在while循环的else部分内没有改变?请告诉我这种差异可能是什么原因.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std;

/* arr[] of size n */

void printNGE(int arr[], int n)
{
    int i = 0;
    stack <int> s;
    int ans[n];
    for(i=0;i<n;i++)//Set all the elements to -1
        ans[i] = -1;

    //memset(ans,-1,sizeof(ans));

    while (i<n)
    {
        if(s.empty() || arr[s.top()]>arr[i]) 
            s.push(i++);
        else
        {
            ans[s.top()]=arr[i];    
            s.pop(); //pop till incoming element is greater
        }
    }
    for(i=0;i<n;i++)
        printf("%d -> %d\n",arr[i],ans[i]);
}

int main()
{
    int arr[]= {11, 13, 21, …
Run Code Online (Sandbox Code Playgroud)

c++

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

C 给出错误的计算答案

我正在用 C 编写一个非常简单的程序,但我正在做的计算总是得到错误的答案。我想要的最终输出需要没有小数位,因此int即使答案不是整数,我也将其用作数据类型。这是代码:

int numberOfInches = (100/254)*101;
Run Code Online (Sandbox Code Playgroud)

如果我将其int用作数据类型,我会得到答案 0,如果我尝试使用float或 ,我会得到疯狂的长数字double。关于我做错了什么的任何想法?

c math

-5
推荐指数
1
解决办法
1451
查看次数

标签 统计

c ×3

c++ ×2

bash ×1

c++11 ×1

embedded ×1

gcc ×1

grep ×1

linux ×1

match ×1

math ×1

openmp ×1

regex ×1

syntax ×1

visual-studio-code ×1