C++ Primer: Chapter 9 Sequential Container

Homework

Test 9.4

bool searchvalue(vector<int> ivec, int i)
{
	vector<int>::iterator begin = ivec.begin(), end = ivec.end();
	while (begin != end)
	{
		if (*begin == i)
			return true;
		else
			++begin;
	}
	if (begin == end)
		return false;
}

Test 9.5

vector<int>::iterator searchvalue(vector<int> ivec, int i)
{
	vector<int>::iterator begin = ivec.begin(), end = ivec.end();
	while (begin != end)
	{
		if (*begin == i)
			return begin;
		else
			++begin;
	}
	if (begin == end)
		return end;
}

Test 9.11

vector<int> ivec1;
vector<int> ivec2(ivec1);
vector<int> ivec3{ 2, 4, 6, 8, 10 };
vector<int> ivec4(ivec3.begin(), ivec3.end());
vector<int> ivec5(5);
vector<int> ivec6(5, 1);

Test 9.13

list<int> lit{ 1,3, 5, 7, 9 };
vector<double> dvec(lit.begin(), lit.end());

Test 9.14

list<const char*> clit{"aaa", "bbb", "ccc"};
vector<string> svec;
svec.assign(clit.cbegin(), clit.cend());

Test 9.16

vector<int> ivec1{ 1, 2, 3, 4, 5 };
list<int> lit{ 1, 2, 3, 4, 5 };
vector<int> ivec3(lit.begin(), lit.end());
if (ivec1 == ivec3)
	cout << "相等" << endl;
else
	cout << "不相等" << endl;

Test 9.18

deque<string> sdq;
string str;
while (cin >> str)
{
	sdq.push_back(str);
}

auto begin = sdq.begin(), end = sdq.end();
while (begin != end)
{
	cout << *begin << endl;
	++begin;
}

Test 9.19

list<string> slit;
string str;
while (cin >> str)
{
	slit.push_back(str);
}

auto begin = slit.begin(), end = slit.end();
while (begin != end)
{
	cout << *begin << endl;
	++begin;
}

Test 9.20

list<int> ilit{ 1, 2, 3, 4, 5, 6, 7, 8 };

deque<int> odddq;
deque<int> evendq;

auto begin = ilit.begin(), end = ilit.end();
while (begin != end)
{
	if (*begin % 2 == 0)
		evendq.push_back(*begin);
	else
		odddq.push_back(*begin);
	++begin;
}

for (auto oit = odddq.begin(); oit != odddq.end(); ++oit)
	cout << *oit << endl;
for (auto eit = evendq.begin(); eit != evendq.end(); ++eit)
	cout << *eit << endl;

Test 9.22

vector<int> iv = { 1, 1, 2, 1 };         
int some_val = 1;

vector<int>::iterator iter = iv.begin();
int org_size = iv.size(), new_ele = 0;  

while (iter != (iv.begin() + org_size / 2 + new_ele))
{
	if (*iter == some_val) {
		iter = iv.insert(iter, 2 * some_val);  
		++new_ele;
		iter += 2;             
	}
	else
		++iter;
}

for (iter = iv.begin(); iter != iv.end(); ++iter)
	cout << *iter << endl;

Test 9.24

vector<int> iv = { 3, 1, 2, 1 };         

cout << iv.at(0) << endl;
cout << iv.front() << endl;
cout << *(iv.begin()) << endl;

Test 9.26

int ia[] = { 0 ,1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };

vector<int> ivec;
list<int> ilit;

for (auto ip = begin(ia); ip != end(ia); ++ip)
{
	ivec.push_back(*ip);
	ilit.push_back(*ip);
}

//由于删除元素会使迭代器失效,因此不能在for循环中使用++it,应在循环体中自行定义迭代条件
for (auto it = ivec.begin(); it != ivec.end(); )
{
	if (*it % 2 == 0)
		it = ivec.erase(it);
	else
		++it;
}

for (auto it = ilit.begin(); it != ilit.end(); )
{
	if (*it % 2 != 0)
		it = ilit.erase(it);
	else
		++it;
}

for (auto it = ivec.begin(); it != ivec.end(); ++it)
	cout << *it << endl;
for (auto it = ilit.begin(); it != ilit.end(); ++it)
	cout << *it << endl;

Test 9.27

forward_list<int> flst{ 0 ,1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };

auto prev = flst.before_begin();
for (auto it = flst.begin(); it != flst.end(); )
{
	if (*it % 2 != 0)
		it = flst.erase_after(prev);
	else
	{
		++prev;
		++it;
	}
}

for (auto it = flst.cbegin(); it != flst.cend(); ++it)
	cout << *it << endl;

Test 9.28

void insertflst(forward_list<string>& flst, string str1, string str2)
{
	bool isIn = false;
	auto prev = flst.before_begin();
	auto it = flst.begin();
	while (it != flst.end())
	{
		if (*it == str1)
		{
			it = flst.insert_after(it, str2);
			++it;
			++prev;
			isIn = true;
		}
		else
		{
			++it;
			++prev;
		}
			
	}

	if (!isIn)
		flst.insert_after(prev, str2);

}

Test 9.38

vector<int> vec;
for (int i = 0; i < 30; ++i)
{
    vec.push_back(i);
    cout << "vec.size() = " << vec.size() << " ";
    cout << "vec.capacity() " << vec.capacity() << endl;
}

Test 9.41

string setstr(vector<char> cvec)
{
    string str;
    for (auto c : cvec)
    {
        str.push_back(c);
    }
    return str;
}

Test 9.43

string replacestr(string s, string oldVal, string newVal)
{
    for (auto i = 0; i<s.size(); ++i)
    {
        string str(s, i, oldVal.size());

        if (str == oldVal)
        {
            s.erase(i, oldVal.size());
            s.insert(i, newVal);
        }
        
    }

    return s;
}

Test 9.44

string replacestr(string s, string oldVal, string newVal)
{
    for (auto i = 0; i<s.size(); ++i)
    {
        string str(s, i, oldVal.size());

        if (str == oldVal)
        {
            s.replace(i, oldVal.size(), newVal);
        }
        
    }

    return s;
}

Test 9.45

string presuffix(string s, string prefix, string suffix)
{
    s.insert(s.begin(), prefix.begin(), prefix.end());
    s.append(suffix);

    return s;
}

Test 9.46

string presuffix(string s, string prefix, string suffix)
{
    s.insert(0, prefix);
    s.insert(s.size(), suffix);

    return s;
}

Test 9.47

void findstr(string str)
{
    string numbers = "0123456789";
    string words = "abcedfghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";

    string::size_type pos = 0;
    while ((pos = str.find_first_of(numbers, pos)) != string::npos)
    {
        cout << "found number at index: " << pos << " elements is: " << str[pos] <<endl;
        ++pos;
    }

    string::size_type pos2 = 0;
    while ((pos2 = str.find_first_of(words, pos2)) != string::npos)
    {
        cout << "found word at index: " << pos2 << " elements is: " << str[pos2] << endl;
        ++pos2;
    }
}
int main() {

    string str = "ab2c3d7R4E6";
    findstr(str);
    
    return 0;
}

Test 9.49

string asdescender(string str)
{
    string ascender = "bdfhiklt";
    string descender = "gjpqy";

    string::size_type pos = 0;
    while ((pos = str.find_first_of(ascender + descender, pos)) != string::npos)
    {
        str.erase(pos, 1);
    }

    return str;
}

Test 9.50

vector<string> svec = { "1.2", "2.9", "3.0", "4.3", "5.3", "10.1", "12.7", "14.1" };

double sum = 0;;
for (auto s : svec)
{
    sum += stod(s);
}

cout << sum << endl;

Test 9.51

class date
{
public:
	unsigned year = 0;
	unsigned month = 0;
	unsigned day = 0;

	date(string& str);
};

date::date(string& str)
{
	//year
	for (int i = 0; i < str.size() - 3; ++i)
	{
		if (isdigit(str[i]) && isdigit(str[i + 1]) && isdigit(str[i + 2]) && isdigit(str[i + 3]))
		{
			year = stoul(str.substr(i, 4));
		}
	}

	//day
	string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string numbers = "0123456789";

	if (isdigit(str[str.find_first_of(numbers) + 1])) //if the next char of the first number is also number
	{
		day = stoul(str.substr(str.find_first_of(numbers), 2)); //the first two numbers is day
	}
	else
		day = stoul(str.substr(str.find_first_of(numbers), 1)); //the first number is day
	

	//month
	if (str.find_first_of(letters) == string::npos) //if no letters
	{
		auto first_num_pos = str.find_first_of(numbers);
		month = stoul(str.substr(str.find_first_of(numbers, first_num_pos + 1), 1)); // the second number is month
	}
	else //exist letters
	{
		vector<string> month_svec = { "January", "February", "March" , "April", "May", "June", "July", "August", "September", "October", "November", "December" };
		vector<string> month_simp_svec = { "Jan", "Feb", "Mar" , "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
		for (unsigned i = 0; i < month_svec.size(); ++i)
		{
			if (str.find(month_svec[i]) != string::npos || str.find(month_simp_svec[i]) != string::npos)
			{
				month = i + 1;
			}
		}

	}
	
}

Test 9.52

string expr = "3 * (16 + 3 * 2) - 18 * (6 + 4) = ";
string result;

stack<char> stk;

int left_num = 0; //左括号出现次数

for (auto c : expr)
{
	stk.push(c);

	if (c == '(')
	{
		++left_num;
	}
	
	if (left_num != 0 && c == ')') //如果遇到右括号
	{
		while (stk.top() != '(')
		{
			stk.pop(); //循环弹出元素直到遇到左括号
		}
		stk.pop();//弹出左括号
		--left_num;
		stk.push('#'); //用#表示计算结果
	}
}

while (!stk.empty())
{
	result.insert(result.begin(), stk.top());
	stk.pop();
	
}

cout << result << endl;
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值