C++ STL:c++;
当我在美国测试的时候。转换函数无法将新值指定给结果。复制函数无法编译。有人能帮我弄清楚那里发生了什么吗 非常感谢你C++ STL:c++;,c++,string,stl,copy,transform,C++,String,Stl,Copy,Transform,当我在美国测试的时候。转换函数无法将新值指定给结果。复制函数无法编译。有人能帮我弄清楚那里发生了什么吗 非常感谢你 // Include standard library headers #include <iostream> using std::cin; using std::cout; using std::endl; #include <ostream> using std::ostream; #include <sstream> using st
// Include standard library headers
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <ostream>
using std::ostream;
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
// Include test headers
#include "TestFramework.h"
#include "UnitTest.h"
// *************** Start the Unit test methods below *******************
void Hw1UnitTest::Initialize(ostream &out)
{
TestFramework::Initialize(&out);
}
void Hw1UnitTest::DisplaySummary()
{
TestFramework::DisplaySummary();
}
int current = 0;
int UniNumber() {return ++current;}
void Hw1UnitTest::TestDeque()
{
TestFramework::BeginTest("Test Deque");
deque<int> myDeck;
int init = 0;
int result;
generate_n(back_inserter(myDeck), 10, UniNumber);
result = accumulate(myDeck.begin(), myDeck.end(), init);
TEST_ASSERT(55 == result);
TestFramework::EndTest();
}
int toUpperCase(int lower) { return toupper(lower); }
void Hw1UnitTest::TestString()
{
TestFramework::BeginTest("Test String");
string sample = "abcdefghijklmnopqrstuvwxyz";
string result;
// problem there's no return value to the result string ****************
transform(sample.begin(), sample.end(), result.begin(), toUpperCase);
TEST_ASSERT("ABCDEFGHIJKLMNOPQRSTUVWXYZ" == result);
TestFramework::EndTest();
}
bool IsEven (int i) { return (i%2) == 0; }
void Hw1UnitTest::TestVector()
{
TestFramework::BeginTest("Test Vector");
int sample[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
vector<int> vector0 (sample, (sample + 10));
vector<int>::iterator bound;
bound = std::partition (vector0.begin(), vector0.end(), IsEven);
sort(vector0.begin(), vector0.begin() + 5);
sort(vector0.begin() + 5, vector0.end());
ostringstream os;
// problem here, can not do copy **************
copy(vector0.begin(), vector0.end(), ostream_iterator<string>(os));
TEST_ASSERT("24681013579" == os.str());
TestFramework::EndTest();
}
//包括标准库头
#包括
使用std::cin;
使用std::cout;
使用std::endl;
#包括
使用std::ostream;
#包括
使用std::stringstream;
#包括
使用std::string;
//包括测试头
#包括“TestFramework.h”
#包括“UnitTest.h”
//*************开始下面的单元测试方法*******************
void Hw1UnitTest::初始化(ostream&out)
{
TestFramework::初始化(&out);
}
void Hw1UnitTest::DisplaySummary()
{
TestFramework::DisplaySummary();
}
int电流=0;
int UniNumber(){return++current;}
void Hw1UnitTest::TestDeque()
{
TestFramework::BeginTest(“测试开发”);
德克·米戴克;
int init=0;
int结果;
生成n(后插入器(myDeck),10,未编号);
结果=累积(myDeck.begin(),myDeck.end(),init);
测试断言(55==结果);
TestFramework::EndTest();
}
int-toUpperCase(int-lower){return-toupper(lower);}
void Hw1UnitTest::TestString()
{
TestFramework::BeginTest(“测试字符串”);
字符串sample=“abcdefghijklmnopqrstuvxyz”;
字符串结果;
//问题:结果字符串没有返回值****************
转换(sample.begin()、sample.end()、result.begin()、toUpperCase);
测试断言(“abcdefghijklmnopqrstuvxyz”=结果);
TestFramework::EndTest();
}
bool-IsEven(inti){return(i%2)=0;}
void Hw1UnitTest::TestVector()
{
TestFramework::BeginTest(“测试向量”);
int sample[]={10,9,8,7,6,5,4,3,2,1};
向量向量0(样本,(样本+10));
向量::迭代器界;
bind=std::partition(vector0.begin()、vector0.end()、IsEven);
排序(vector0.begin(),vector0.begin()+5);
排序(vector0.begin()+5,vector0.end());
ostringstream os;
//这里有问题,不能复印**************
复制(vector0.begin()、vector0.end()、ostream_迭代器(os));
TEST_ASSERT(“24681013579”==os.str());
TestFramework::EndTest();
}
更改此语句
copy(vector0.begin(), vector0.end(), ostream_iterator<string>(os));
copy( vector0.begin(), vector0.end(), ostream_iterator<string>( os));
到
问题是对象result
为空,您不能使用迭代器begin
输入新元素。
你可以使用你的代码
string sample = "abcdefghijklmnopqrstuvwxyz";
string result;
// problem there's no return value to the result string ****************
transform(sample.begin(), sample.end(), result.begin(), toUpperCase);
如果您将结果定义为
string sample = "abcdefghijklmnopqrstuvwxyz";
string result( sample.size() );
您的std::transform()
无法工作,因为目标需要足够大才能接收输出,而目标是空字符串。您可以使用std::back\u插入器
,这样算法将将新值推送到目标
改变
transform(sample.begin(), sample.end(), result.begin(), toUpperCase);
到
在副本声明中
copy(vector0.begin(), vector0.end(), ostream_iterator<string>(os));
copy( vector0.begin(), vector0.end(), ostream_iterator<string>( os));
关于std::transform
,result
为空,因此您必须为字符串预先分配空间:
string sample = "abcdefghijklmnopqrstuvwxyz";
string result( sample.size());
transform( sample.begin(), sample.end(), result.begin(), toUpperCase);
或者使用back\u inserter
将元素添加到当前没有元素空间的字符串中
transform( sample.begin(), sample.end(), back_inserter(result), toUpperCase);
但问题也可能与toUpperCase
函数有关,您没有显示该函数的实现。请显示编译器报告的错误,并且toUpperCase函数在使用using
带来新的(可能非常常见和/或简短!)标识到全局命名空间中。请发布错误信息以获得更好的答案
copy( vector0.begin(), vector0.end(), ostream_iterator<int>( os));
string sample = "abcdefghijklmnopqrstuvwxyz";
string result( sample.size());
transform( sample.begin(), sample.end(), result.begin(), toUpperCase);
transform( sample.begin(), sample.end(), back_inserter(result), toUpperCase);