string pour[100]; // 或 vector<string> pour(100);
for (int i = 0; i < a; i++) {
scanf("%s", pour[i]); // 編譯錯誤或運行崩潰
}
錯誤訊息:cannot convert 'std::string*' to 'char*' 或 運行時 Access Violation。
scanf。char* pour[100],每個指標需先 malloc/new 配置空間。cin(推薦)vector<string> pour(a);
for (int i = 0; i < a; i++) {
cin >> pour[i]; // 自動處理記憶體、長度、換行
}
vector<string> + resize + &str[0]vector<string> pour(a);
for (int i = 0; i < a; i++) {
pour[i].resize(100); // 預留 100 字元緩衝區
scanf("%99s", &pour[i][0]); // 讀入底層 char 陣列,限寬防溢位
pour[i].shrink_to_fit(); // 可選:縮減多餘容量
}
&pour[i][0] 取得內部連續記憶體起始位址 (C++11 保證連續)。%99s 限制讀取長度,留 1 位給 \0。char pour[100][100]char pour[100][100];
for (int i = 0; i < a; i++) {
scanf("%99s", pour[i]); // pour[i] 退化為 char*
}
#include <iostream>
#include <vector>
#include <stack>
#include <string>
using namespace std;
bool isValid(const string& s) {
stack<char> st;
for (char c : s) {
if (c == '(' || c == '[' || c == '{') st.push(c);
else if (c == ')' || c == ']' || c == '}') {
if (st.empty()) return false;
char top = st.top(); st.pop();
if ((c == ')' && top != '(') ||
(c == ']' && top != '[') ||
(c == '}' && top != '{')) return false;
}
}
return st.empty();
}
int main() {
int n; cin >> n;
vector<string> lines(n);
for (int i = 0; i < n; i++) cin >> lines[i];
for (auto& line : lines)
cout << (isValid(line) ? "Match" : "Mismatch") << '\n';
}
拒絕 C 風格字串操作,擁抱 std::string + iostream,才是現代 C++ 正道。