What could be causing the 'expected expression' error when pushing a std::pair to a vector in my graph code using VSCode?

huangapple go评论71阅读模式
英文:

What could be causing the 'expected expression' error when pushing a std::pair to a vector in my graph code using VSCode?

问题

错误:预期表达式
在使用VSCode将std::pair推送到vector时,如何修复“预期表达式”错误?

要修复这个错误,你可以将 #include <iostream>#include <vector> 这两行代码中的角括号 <> 改成尖括号 <>,同时将 #define ii pair<int, int> 中的角括号也改成尖括号,以确保正确的包含头文件和定义 ii 类型。修正后的代码如下:

#include <iostream>
#include <sstream>
#include <vector>
#define ii pair<int, int>
using namespace std;

// DSK -> DSC
int n;
vector<ii> edge;
string s, num;

int main()
{
    cin >> n;
    cin.ignore();
    for(int i = 1; i <= n; i++)
    {
        getline(cin, s);
        stringstream ss(s);
        while(ss >> num)
            if(i < stoi(num))
                edge.push_back({i, stoi(num)});
    }
    return 0;
}

请注意,这些角括号 <> 是C++中用于包含头文件和模板参数的标准用法。修复这些角括号后,代码应该能够编译通过了。

英文:
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;
#define ii pair&lt;int, int&gt;
using namespace std;

// DSK -&gt; DSC
int n;
vector &lt;ii&gt; edge;
string s, num;

int main()
{
    cin &gt;&gt; n;
    cin.ignore();
    for(int i = 1; i &lt;= n; i++)
    {
        getline(cin, s);
        stringstream ss(s);
        while(ss &gt;&gt; num)
            if(i &lt; stoi(num))
                edge.push_back({i, stoi(num)});
    }
    return 0;
}
error: expected expression
                edge.push_back({i, stoi(num)});
                               ^

How can I fix the 'expected expression' error in my graph code when using VSCode to push a std::pair to a vector?

答案1

得分: 5

My psychic powers suggest you are on an older version of C++. Maybe C++11? Not sure which compiler you are using, but there are command line flags that will set the compiler environment to a newer standard than the default.

clang/g++: -std=c++17

MSVC: /std:c++17

Regardless, there's simpler ways to solve this than fighting the compiler. Just make it happy by giving it the type it expects instead of it trying to infer how to coerce your inline params.

For what's really just a couple of ints, this won't incur any performance penalty. And the compiler will likley optimize out the extra copy (if any) in a retail build anyway:

Instead of this:

edge.push_back({i, stoi(num)});

This:

std::pair<int,int> p = {i, stoi(num)};
edge.push_back(p);

Pro-tip unrelated to your quesiton. Always use curly braces for nested statements, even if it's just a single line. It will prevent bugs that easily occur later for not having a block explicitly declared. Instead of this:

        while(ss >> num)
            if(i < stoi(num))
                edge.push_back({i, stoi(num)});

This:

        while(ss >> num)
        {
            if(i < stoi(num))
            {
                edge.push_back({i, stoi(num)});
            }
        }

Putting it altogether:

while(ss >> num)
{
if(i < stoi(num))
{
std::pair<int,int> p = {i, stoi(num)};
edge.push_back(p);
}
}

The other answers and comments sugggesting to use make_pair or emplace_back are good suggestions too.

英文:

My psychic powers suggest you are on an older version of C++. Maybe C++11? Not sure which compiler you are using, but there are command line flags that will set the compiler environment to a newer standard than the default.

clang/g++: -std=c++17

MSVC: /std:c++17

Regardless, there's simpler ways to solve this than fighting the compiler. Just make it happy by giving it the type it expects instead of it trying to infer how to coerce your inline params.

For what's really just a couple of ints, this won't incur any performance penalty. And the compiler will likley optimize out the extra copy (if any) in a retail build anyway:

Instead of this:

edge.push_back({i, stoi(num)});

This:

std::pair&lt;int,int&gt; p = {i, stoi(num)};
edge.push_back(p);

Pro-tip unrelated to your quesiton. Always use curly braces for nested statements, even if it's just a single line. It will prevent bugs that easily occur later for not having a block explicitly declared. Instead of this:

        while(ss &gt;&gt; num)
            if(i &lt; stoi(num))
                edge.push_back({i, stoi(num)});

This:

        while(ss &gt;&gt; num)
        {
            if(i &lt; stoi(num))
            {
                edge.push_back({i, stoi(num)});
            }
        }

Putting it altogether:

    while(ss &gt;&gt; num)
    {
        if(i &lt; stoi(num))
        {
            std::pair&lt;int,int&gt; p = {i, stoi(num)};
            edge.push_back(p);
        }
    }

The other answers and comments sugggesting to use make_pair or emplace_back are good suggestions too.

答案2

得分: 1

我认为你可以使用make_pair()函数。

edge.push_back(make_pair(i, stoi(num)));

英文:

I think you can use make_pair() function.

edge.push_back(make_pair(i, stoi(num)));

huangapple
  • 本文由 发表于 2023年5月30日 12:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定