IO操作
输入示例:第一行个数,后面为成对的值
3 0 0 1 0 2 0
int Test_Main() { int nline; vector<pair<int,int>> data; cin >> nline; while (nline > 0) { int l, r; cin >> l >> r; data.push_back(make_pair(l,r)); nline--; } int count = GetPointCount(data); cout << count << endl; }
or
int n;
cin >> n;
vector<pair<int, int>> points;
for (int i = 0; i < n; ++i) {
int x;
int y;
cin >> x >> y;
points.push_back(make_pair(x, y));
}
//string str;
//getline(cin, str);
//cout << "user input" << endl;
//cout << "getline:" << str << endl;
void TestInput() { int num; cin >> num; cin.ignore(); vector<string> fileTreeList; for (int i = 0; i < num; ++i) { string oneLine = ReadLine(); fileTreeList.push_back(oneLine); } }
分割字符串
class StringExample { public: void ShowList(const vector<string> &lst) { for (size_t i = 0; i < lst.size(); ++i) { cout << lst[i] << " "; } cout << endl; } // 分割字符串 /drv/files/log/init.log void SubString1(string filepath) { cout << filepath << ": "; vector<string> lst1; istringstream fileName(filepath); string name; while (getline(fileName, name, '/')) { lst1.push_back(name); name.clear(); } ShowList(lst1); } }; TEST(StringExample, T1) { StringExample st; st.SubString1("usr/local/lib64/"); st.SubString1("/drv/files/log/init.log"); }
输出:
usr/local/lib64/: usr local lib64
/drv/files/log/init.log: drv files log init.log
第二种
void DealFileName(const string input, vector<string> &output, set<string> &flag) { string str(input); while (!str.empty() && str[str.size() - 1] == '/') { str.pop_back(); } size_t pos = 0; int cnt = 0; while (pos != string::npos) { string strTmp; size_t idx = str.find('/', pos); if (idx != string::npos) { strTmp += str.substr(pos, idx - pos); if (flag.count(strTmp) == 0) { output.push_back(strTmp); flag.insert(strTmp); } cnt++; pos = idx + 1; } else { strTmp += str.substr(pos); if (flag.count(strTmp) == 0) { output.push_back(strTmp); flag.insert(strTmp); } break; } } }
子网掩码匹配
class Solution { public: long Ipv4StringToInt(string ipv4) { long cnt = 0; istringstream is(ipv4); string str; while (getline(is, str, '.')) { istringstream ss(str); int val; ss >> val; cnt = cnt * 1000 + val; str.clear(); } return cnt; } long BuildMask(string ipv4mask, int &masklen) { size_t pos = ipv4mask.find('/'); string ip = ipv4mask.substr(0, pos); string mlen = ipv4mask.substr(pos + 1); istringstream ss(mlen); ss >> masklen; return Ipv4StringToInt(ip); } bool TestIpv4Mask(string ipv4, string mask, int &masklen) { long ip = Ipv4StringToInt(ipv4); long maskip = BuildMask(mask, masklen); ip = ip >> masklen; maskip = maskip >> masklen; if ((maskip ^ ip) == 0) { return true; } return false; } // 待实现函数,在此函数中填入答题代码; string RouterSearch(const string &dstIp, const vector<string> &ipTable) { string result; int len = 0; string def = "0.0.0.0/0"; bool isdefault = false; for (size_t i = 0; i < ipTable.size(); ++i) { int nlen = 0; if (TestIpv4Mask(dstIp, ipTable[i], nlen)) { if (nlen > len) { len = nlen; result = ipTable[i]; } } if (ipTable[i] == def) { isdefault = true; } } if (isdefault && result.empty()) { result = def; } return result; } };
浙公网安备 33010602011771号