英文:
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 <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;
}
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<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.
答案2
得分: 1
我认为你可以使用make_pair()函数。
edge.push_back(make_pair(i, stoi(num)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论