题目
输入一个字符串
info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]"
说明
-
上面的字符串实际来自一个字典结构的数据
-
key: '1001'
-
value: [ [a1,b1,c1], [a2,b2,c2], ... , [an,bn,cn] ]
-
a是整数
-
b是浮点数
-
c是字符串
-
-
要求
输出一个字典结构的数据
-
key是字符串
-
value等价于列表形式,这个列表的元素又是一个列表形式
-
列表:第1个是整数,第2个是浮点数,第3个是字符串
-
如下形式
info = {'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]}
初步思路-1
-
观察发现字符串':符号只出现在key值的后面
-
以符号':查找字符串
#include <iostream>
using namespace std;
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
int index = info.find("':");
cout << index << endl;
}
5
根据结果看找到了字符串第一次出现':的索引
初步思路-2
-
设置一个变量记录':出现的索引位置,每次查找字符串的开始位置在该索引位置+1后
-
设置一个变量记录是否成功找到':,作为循环的条件
先看下效果
#include <iostream>
using namespace std;
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
int mark = -1;
int index = info.find("':",mark+1);
cout << index <<": "<<info[index-4]<<info[index-3]
<<info[index-2]<<info[index-1]<<endl;
mark = index;
index = info.find("':",mark+1);
cout << index <<": "<<info[index-4]<<info[index-3]
<<info[index-2]<<info[index-1]<<endl;
}
5: 1001
140: 1002
加入循环
#include <iostream>
using namespace std;
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
int mark = -1;
bool have = true;
int index;
while (have)
{
index = info.find("':",mark+1);
if (index != -1)
{
cout << index <<": "<<info[index-4]<<info[index-3]
<<info[index-2]<<info[index-1]<<endl;
mark = index;
}
else { have = false; }
}
}
5: 1001
140: 1002
233: 1003
初步思路-3
-
在初步思路-2的基础上,查找key的部分写出函数
-
同时记录每次查找到的索引位置
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
auto get_key_index(string info)
{
int mark = -1;
bool have = true;
int index;
vector<string> vecKeys;
vector<int> keyIndex;
while (have)
{
index = info.find("':",mark+1);
if (index != -1)
{
vecKeys.push_back(info.substr(index-4,4));
keyIndex.push_back(index);
mark = index;
}
else { have = false; }
}
pair <vector<string>,vector<int>> out = {vecKeys,keyIndex};
return out;
}
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
for (auto key:vecKeys) {cout<<key<<", ";}
cout << endl;
for (auto index:keyIndex) {cout<<index<<", ";}
cout << endl;
}
1001, 1002, 1003,
5, 140, 233,
初步思路-4
-
在初步思路-3的基础上,除了得到了key,还得到了每个key后的索引位置
-
2个索引位置之间的是value
-
最后一个value是最后一个key后的索引位置到字符串的末尾
先把初步思路-3得到的索引位置变成一对一对的形式
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
auto get_key_index(string info)
{
int mark = -1;
bool have = true;
int index;
vector<string> vecKeys;
vector<int> keyIndex;
while (have)
{
index = info.find("':",mark+1);
if (index != -1)
{
vecKeys.push_back(info.substr(index-4,4));
keyIndex.push_back(index);
mark = index;
}
else { have = false; }
}
pair <vector<string>,vector<int>> out = {vecKeys,keyIndex};
return out;
}
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
for (auto index:keyIndex) {cout<<index<<", ";}
cout << "----------"<<endl;
vector<pair<int,int>> pairIndex;
for (int i=0;i<keyIndex.size();i++)
{
if (i<keyIndex.size()-1) {pairIndex.push_back({keyIndex[i],keyIndex[i+1]}); }
else {pairIndex.push_back({keyIndex[i],info.size()-1});}
}
for (auto p:pairIndex) {
cout<<"("<<p.first<<","<<p.second<<")"<<endl;
}
}
5, 140, 233, ----------
(5,140)
(140,233)
(233,276)
根据value的起止位置,看下效果,下面代码只贴main函数部分
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<pair<int,int>> pairIndex;
for (int i=0;i<keyIndex.size();i++)
{
if (i<keyIndex.size()-1) {pairIndex.push_back({keyIndex[i],keyIndex[i+1]}); }
else {pairIndex.push_back({keyIndex[i],info.size()-1});}
}
for (auto p:pairIndex) {
cout << info.substr(p.first,p.second-p.first) << endl;
}
}
':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002
':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003
':[[0,1.0,'http://10.8:90/name/1001_0.jpg']
根据打印效果,发现
-
':符号在里面,key值也在里面
-
最后一行少了一个]符号
main部分代码如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<pair<int,int>> pairIndex;
for (int i=0;i<keyIndex.size();i++)
{
if (i<keyIndex.size()-1) {pairIndex.push_back({keyIndex[i]+2,keyIndex[i+1]}); }
else {pairIndex.push_back({keyIndex[i]+2,info.size()+6});}
}
for (auto p:pairIndex) {
cout << info.substr(p.first,p.second-p.first-6) << endl;
}
}
[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']]
[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']]
[[0,1.0,'http://10.8:90/name/1001_0.jpg']]
注意:上面其实还是3行字符串
设计一个string类型的vector,存上面的行字符串,并写成函数
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
auto get_key_index(string info)
{
int mark = -1;
bool have = true;
int index;
vector<string> vecKeys;
vector<int> keyIndex;
while (have)
{
index = info.find("':",mark+1);
if (index != -1)
{
vecKeys.push_back(info.substr(index-4,4));
keyIndex.push_back(index);
mark = index;
}
else { have = false; }
}
pair <vector<string>,vector<int>> out = {vecKeys,keyIndex};
return out;
}
auto get_strValue(vector<int> keyIndex, string info)
{
vector<pair<int,int>> pairIndex;
for (int i=0;i<keyIndex.size();i++)
{
if (i<keyIndex.size()-1) {pairIndex.push_back({keyIndex[i]+2,keyIndex[i+1]}); }
else {pairIndex.push_back({keyIndex[i]+2,info.size()+6});}
}
vector<string> strValue;
for (auto p:pairIndex) {
strValue.push_back(info.substr(p.first,p.second-p.first-6));
}
return strValue;
}
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<string> strValue = get_strValue(keyIndex,info);
for (auto str:strValue) {cout<<str<<endl;}
}
[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']]
[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']]
[[0,1.0,'http://10.8:90/name/1001_0.jpg']]
初步思路-5
-
在初步思路-4的基础上,我们已经得到了3行字符串
-
思路1: 用[]符号拆分字符串
-
思路2: 用字符串中每3个元素组成一个list的规律拆分字符串
-
接下来我们用思路2试一试
先去除字符串中的[]'符号
在main里面继续写代码
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<string> strValue = get_strValue(keyIndex,info);
string temp = "";
for (int i=0;i<strValue.size();i++)
{
for (auto c:strValue[i])
{
if (c != '[' and c != ']' and c != '\'') { temp += c; }
}
strValue[i] = temp;
temp = "";
}
for (auto strSlim:strValue) {cout<<strSlim<<endl;}
}
0,1.0,http://10.8:90/name/1001_0.jpg,10,0.7,http://10.8:90/name/1001_1.jpg,20,0.85,http://10.8:90/name/1001_2.jpg
0,1.0,http://10.8:90/name/1002_0.jpg,12,0.75,http://10.8:90/name/1002_1.jpg
0,1.0,http://10.8:90/name/1001_0.jpg
里我们实现了一个按字符把字符串分隔成vector的函数,修改函数如下
auto s_split(string str, char s)
{
vector<string> list;
string temp;
for (int i=0; i<str.size(); i++)
{
if (str[i] != s)
{ temp = temp + str[i]; }
else
{ list.push_back(temp);
temp = ""; }
if (i==str.size()-1)
{ list.push_back(temp); }
}
return list;
}
main里如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<string> strValue = get_strValue(keyIndex,info);
string temp = "";
for (int i=0;i<strValue.size();i++)
{
for (auto c:strValue[i])
{
if (c != '[' and c != ']' and c != '\'') { temp += c; }
}
strValue[i] = temp;
temp = "";
}
vector<vector<string>> valueSplit;
for (auto strSlim:strValue) {
valueSplit.push_back(s_split(strSlim,','));
}
for (auto vec:valueSplit) {
for (auto element:vec) { cout << element << " | "; }
cout << endl;
}
}
结果如下
0 | 1.0 | http://10.8:90/name/1001_0.jpg | 10 | 0.7 | http://10.8:90/name/1001_1.jpg | 20 | 0.85 | http://10.8:90/name/1001_2.jpg |
0 | 1.0 | http://10.8:90/name/1002_0.jpg | 12 | 0.75 | http://10.8:90/name/1002_1.jpg |
0 | 1.0 | http://10.8:90/name/1001_0.jpg |
写成函数
auto get_splitValue(vector<int> keyIndex,string info)
{
vector<string> strValue = get_strValue(keyIndex,info);
string temp = "";
for (int i=0;i<strValue.size();i++)
{
for (auto c:strValue[i])
{
if (c != '[' and c != ']' and c != '\'') { temp += c; }
}
strValue[i] = temp;
temp = "";
}
vector<vector<string>> valueSplit;
for (auto strSlim:strValue) {
valueSplit.push_back(s_split(strSlim,','));
}
return valueSplit;
}
main调用如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
for (auto vec:valueSplit) {
for (auto element:vec) { cout << element << " | "; }
cout << endl;
}
}
结果如下
0 | 1.0 | http://10.8:90/name/1001_0.jpg | 10 | 0.7 | http://10.8:90/name/1001_1.jpg | 20 | 0.85 | http://10.8:90/name/1001_2.jpg |
0 | 1.0 | http://10.8:90/name/1002_0.jpg | 12 | 0.75 | http://10.8:90/name/1002_1.jpg |
0 | 1.0 | http://10.8:90/name/1001_0.jpg |
初步思路-6
在初步思路-5的基础上,得到如上3行vector,它的形式 已经和 python 的list 形式一致了。
继续如下2个操作
-
打印出的每行vector,每3个元素组成一组
-
一组中第一个是整型,第二个是实数,第三个是字符串
先实现每3个元素做一次区分
main里如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
int mark = 0;
for (auto vec:valueSplit) {
for (auto element:vec) {
if (mark==0) {cout<<"first->"<<element<<" | ";}
if (mark==1) {cout<<"second->"<<element<<" | ";}
if (mark==2) {cout<<"third->"<<element<<" | ";}
mark += 1;
if (mark==3) {mark=0;}
}
mark=0;
cout << endl;
}
}
效果如下
first->0 | second->1.0 | third->http://10.8:90/name/1001_0.jpg | first->10 | second->0.7 | third->http://10.8:90/name/1001_1.jpg | first->20 | second->0.85 | third->http://10.8:90/name/1001_2.jpg |
first->0 | second->1.0 | third->http://10.8:90/name/1002_0.jpg | first->12 | second->0.75 | third->http://10.8:90/name/1002_1.jpg |
first->0 | second->1.0 | third->http://10.8:90/name/1001_0.jpg |
再实现string转int、float
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
int mark = 0;
int first; float second; string third;
for (auto vec:valueSplit) {
for (auto element:vec) {
if (mark==0) {
stringstream toInt(element);
toInt >> first;
cout<<"first-("<<typeid(first).name()<<")"<<">"<<first<<" | ";}
if (mark==1) {
stringstream toFloat(element);
toFloat >> second;
cout<<"second-("<<typeid(second).name()<<")"<<">"<<second<<" | ";}
if (mark==2) {
third = element;
cout<<"third-("<<typeid(third).name()<<")"<<">"<<third<<" | ";}
mark += 1;
if (mark==3) {mark=0;}
}
mark=0;
cout << endl;
}
}
效果如下
first-(i)>0 | second-(f)>1 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1001_0.jpg | first-(i)>10 | second-(f)>0.7 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1001_1.jpg | first-(i)>20 | second-(f)>0.85 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1001_2.jpg |
first-(i)>0 | second-(f)>1 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1002_0.jpg | first-(i)>12 | second-(f)>0.75 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1002_1.jpg |
first-(i)>0 | second-(f)>1 | third-(NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)>http://10.8:90/name/1001_0.jpg |
设计一个vector<tuple>类型的vector保存上面的(first, second, third),并写成函数
auto get_vector(vector<vector<string>> valueSplit)
{
vector<vector<tuple<int,float,string>>> multiVec;
vector<tuple<int,float,string>> temp;
int mark = 0;
int first; float second; string third;
for (auto vec:valueSplit) {
for (auto element:vec) {
if (mark==0) {
stringstream toInt(element);
toInt >> first;
}
if (mark==1) {
stringstream toFloat(element);
toFloat >> second;
}
if (mark==2) { third = element; }
mark += 1;
if (mark==3) {
temp.push_back({first,second,third});
mark=0;}
}
multiVec.push_back(temp);
temp.clear();
mark=0;
}
return multiVec;
}
main里代码如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
vector<vector<tuple<int,float,string>>> multiVec = get_vector(valueSplit);
int first; float second; string third;
for (auto line:multiVec)
{
for (auto three:line)
{
tie(first,second,third) = three;
cout<<"("<<first<<","<<second<<","<<third<<") ";
}
cout << endl;
}
}
效果如下
(0,1,http://10.8:90/name/1001_0.jpg) (10,0.7,http://10.8:90/name/1001_1.jpg) (20,0.85,http://10.8:90/name/1001_2.jpg)
(0,1,http://10.8:90/name/1002_0.jpg) (12,0.75,http://10.8:90/name/1002_1.jpg)
(0,1,http://10.8:90/name/1001_0.jpg)
初步思路-7
在初步思路-3得到了keys,上面得到了values
设计一个map,保存keys, values
main里代码如下
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
vector<vector<tuple<int,float,string>>> multiVec = get_vector(valueSplit);
map<string, vector<tuple<int,float,string>>> dict;
for (int i=0; i<vecKeys.size(); i++)
{ dict[vecKeys[i]] = multiVec[i]; }
string key; int first; float second; string third;
for (auto item:dict)
{
key = item.first;
cout<<key<<": ";
for (auto elements:item.second){
tie(first,second,third) = elements;
cout<<"("<<first<<","<<second<<","<<third<<"), ";
}
cout<<endl;
}
}
1001: (0,1,http://10.8:90/name/1001_0.jpg), (10,0.7,http://10.8:90/name/1001_1.jpg), (20,0.85,http://10.8:90/name/1001_2.jpg),
1002: (0,1,http://10.8:90/name/1002_0.jpg), (12,0.75,http://10.8:90/name/1002_1.jpg),
1003: (0,1,http://10.8:90/name/1001_0.jpg),
跟原始字符串对比
"'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]"
满足题目要求
完整代码
#include <iostream>
#include <vector>
#include <tuple>
#include <sstream>
#include <typeinfo>
#include <map>
using namespace std;
auto get_key_index(string info)
{
int mark = -1;
bool have = true;
int index;
vector<string> vecKeys;
vector<int> keyIndex;
while (have)
{
index = info.find("':",mark+1);
if (index != -1)
{
vecKeys.push_back(info.substr(index-4,4));
keyIndex.push_back(index);
mark = index;
}
else { have = false; }
}
pair <vector<string>,vector<int>> out = {vecKeys,keyIndex};
return out;
}
auto get_strValue(vector<int> keyIndex, string info)
{
vector<pair<int,int>> pairIndex;
for (int i=0;i<keyIndex.size();i++)
{
if (i<keyIndex.size()-1) {pairIndex.push_back({keyIndex[i]+2,keyIndex[i+1]}); }
else {pairIndex.push_back({keyIndex[i]+2,info.size()+6});}
}
vector<string> strValue;
for (auto p:pairIndex) {
strValue.push_back(info.substr(p.first,p.second-p.first-6));
}
return strValue;
}
auto s_split(string str, char s)
{
vector<string> list;
string temp;
for (int i=0; i<str.size(); i++)
{
if (str[i] != s)
{ temp = temp + str[i]; }
else
{ list.push_back(temp);
temp = ""; }
if (i==str.size()-1)
{ list.push_back(temp); }
}
return list;
}
auto get_splitValue(vector<int> keyIndex,string info)
{
vector<string> strValue = get_strValue(keyIndex,info);
string temp = "";
for (int i=0;i<strValue.size();i++)
{
for (auto c:strValue[i])
{
if (c != '[' and c != ']' and c != '\'') { temp += c; }
}
strValue[i] = temp;
temp = "";
}
vector<vector<string>> valueSplit;
for (auto strSlim:strValue) {
valueSplit.push_back(s_split(strSlim,','));
}
return valueSplit;
}
auto get_vector(vector<vector<string>> valueSplit)
{
vector<vector<tuple<int,float,string>>> multiVec;
vector<tuple<int,float,string>> temp;
int mark = 0;
int first; float second; string third;
for (auto vec:valueSplit) {
for (auto element:vec) {
if (mark==0) {
stringstream toInt(element);
toInt >> first;
}
if (mark==1) {
stringstream toFloat(element);
toFloat >> second;
}
if (mark==2) { third = element; }
mark += 1;
if (mark==3) {
temp.push_back({first,second,third});
mark=0;}
}
multiVec.push_back(temp);
temp.clear();
mark=0;
}
return multiVec;
}
int main()
{
string info = "'1001':[[0,1.0,'http://10.8:90/name/1001_0.jpg'],[10,0.7,'http://10.8:90/name/1001_1.jpg'],[20,0.85,'http://10.8:90/name/1001_2.jpg']],'1002':[[0,1.0,'http://10.8:90/name/1002_0.jpg'],[12,0.75,'http://10.8:90/name/1002_1.jpg']],'1003':[[0,1.0,'http://10.8:90/name/1001_0.jpg']]";
vector<string> vecKeys; vector<int> keyIndex;
tie(vecKeys,keyIndex) = get_key_index(info);
vector<vector<string>> valueSplit = get_splitValue(keyIndex,info);
vector<vector<tuple<int,float,string>>> multiVec = get_vector(valueSplit);
map<string, vector<tuple<int,float,string>>> dict;
for (int i=0; i<vecKeys.size(); i++)
{ dict[vecKeys[i]] = multiVec[i]; }
string key; int first; float second; string third;
for (auto item:dict)
{
key = item.first;
cout<<key<<": ";
for (auto elements:item.second){
tie(first,second,third) = elements;
cout<<"("<<first<<","<<second<<","<<third<<"), ";
}
cout<<endl;
}
}